2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
31 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by Adam Glass and Charles
45 * 4. The names of the authors may not be used to endorse or promote products
46 * derived from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
52 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 #include <sys/appleapiopts.h>
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/shm_internal.h>
66 #include <sys/proc_internal.h>
67 #include <sys/kauth.h>
68 #include <sys/malloc.h>
71 #include <sys/sysctl.h>
73 #include <sys/sysent.h>
74 #include <sys/sysproto.h>
76 #include <bsm/audit_kernel.h>
78 #include <mach/mach_types.h>
79 #include <mach/vm_inherit.h>
80 #include <mach/vm_map.h>
82 #include <mach/mach_vm.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_shared_memory_server.h>
86 #include <vm/vm_protos.h>
88 #include <kern/locks.h>
90 static void shminit(void *);
92 SYSINIT(sysv_shm
, SI_SUB_SYSV_SHM
, SI_ORDER_FIRST
, shminit
, NULL
)
95 static lck_grp_t
*sysv_shm_subsys_lck_grp
;
96 static lck_grp_attr_t
*sysv_shm_subsys_lck_grp_attr
;
97 static lck_attr_t
*sysv_shm_subsys_lck_attr
;
98 static lck_mtx_t sysv_shm_subsys_mutex
;
100 #define SYSV_SHM_SUBSYS_LOCK() lck_mtx_lock(&sysv_shm_subsys_mutex)
101 #define SYSV_SHM_SUBSYS_UNLOCK() lck_mtx_unlock(&sysv_shm_subsys_mutex)
103 static int oshmctl(void *p
, void *uap
, void *retval
);
104 static int shmget_allocate_segment(struct proc
*p
, struct shmget_args
*uap
, int mode
, int * retval
);
105 static int shmget_existing(struct shmget_args
*uap
, int mode
, int segnum
, int * retval
);
106 static void shmid_ds_64to32(struct user_shmid_ds
*in
, struct shmid_ds
*out
);
107 static void shmid_ds_32to64(struct shmid_ds
*in
, struct user_shmid_ds
*out
);
109 /* XXX casting to (sy_call_t *) is bogus, as usual. */
110 static sy_call_t
*shmcalls
[] = {
111 (sy_call_t
*)shmat
, (sy_call_t
*)oshmctl
,
112 (sy_call_t
*)shmdt
, (sy_call_t
*)shmget
,
116 #define SHMSEG_FREE 0x0200
117 #define SHMSEG_REMOVED 0x0400
118 #define SHMSEG_ALLOCATED 0x0800
119 #define SHMSEG_WANTED 0x1000
121 static int shm_last_free
, shm_nused
, shm_committed
;
122 struct user_shmid_ds
*shmsegs
; /* 64 bit version */
123 static int shm_inited
= 0;
126 void * shm_object
; /* vm_offset_t kva; */
129 struct shmmap_state
{
130 mach_vm_address_t va
; /* user address */
131 int shmid
; /* segment id */
134 static void shm_deallocate_segment(struct user_shmid_ds
*);
135 static int shm_find_segment_by_key(key_t
);
136 static struct user_shmid_ds
*shm_find_segment_by_shmid(int);
137 static int shm_delete_mapping(struct proc
*, struct shmmap_state
*, int);
139 #ifdef __APPLE_API_PRIVATE
140 struct shminfo shminfo
= {
141 -1, /* SHMMAX 4096 *1024 */
145 -1 /* SHMALL = 1024 */
147 #endif /* __APPLE_API_PRIVATE */
149 void sysv_shm_lock_init(void);
151 static __inline__
time_t
160 * This conversion is safe, since if we are converting for a 32 bit process,
161 * then it's value of (struct shmid_ds)->shm_segsz will never exceed 4G.
163 * NOTE: Source and target may *NOT* overlap! (target is smaller)
166 shmid_ds_64to32(struct user_shmid_ds
*in
, struct shmid_ds
*out
)
168 out
->shm_perm
= in
->shm_perm
;
169 out
->shm_segsz
= (size_t)in
->shm_segsz
;
170 out
->shm_lpid
= in
->shm_lpid
;
171 out
->shm_cpid
= in
->shm_cpid
;
172 out
->shm_nattch
= in
->shm_nattch
;
173 out
->shm_atime
= in
->shm_atime
;
174 out
->shm_dtime
= in
->shm_dtime
;
175 out
->shm_ctime
= in
->shm_ctime
;
176 out
->shm_internal
= CAST_DOWN(void *,in
->shm_internal
);
180 * NOTE: Source and target may are permitted to overlap! (source is smaller);
181 * this works because we copy fields in order from the end of the struct to
185 shmid_ds_32to64(struct shmid_ds
*in
, struct user_shmid_ds
*out
)
187 out
->shm_internal
= CAST_USER_ADDR_T(in
->shm_internal
);
188 out
->shm_ctime
= in
->shm_ctime
;
189 out
->shm_dtime
= in
->shm_dtime
;
190 out
->shm_atime
= in
->shm_atime
;
191 out
->shm_nattch
= in
->shm_nattch
;
192 out
->shm_cpid
= in
->shm_cpid
;
193 out
->shm_lpid
= in
->shm_lpid
;
194 out
->shm_segsz
= (user_size_t
)in
->shm_segsz
;
195 out
->shm_perm
= in
->shm_perm
;
200 shm_find_segment_by_key(key_t key
)
204 for (i
= 0; i
< shminfo
.shmmni
; i
++)
205 if ((shmsegs
[i
].shm_perm
.mode
& SHMSEG_ALLOCATED
) &&
206 shmsegs
[i
].shm_perm
.key
== key
)
211 static struct user_shmid_ds
*
212 shm_find_segment_by_shmid(int shmid
)
215 struct user_shmid_ds
*shmseg
;
217 segnum
= IPCID_TO_IX(shmid
);
218 if (segnum
< 0 || segnum
>= shminfo
.shmmni
)
220 shmseg
= &shmsegs
[segnum
];
221 if ((shmseg
->shm_perm
.mode
& (SHMSEG_ALLOCATED
| SHMSEG_REMOVED
))
222 != SHMSEG_ALLOCATED
||
223 shmseg
->shm_perm
.seq
!= IPCID_TO_SEQ(shmid
))
229 shm_deallocate_segment(struct user_shmid_ds
*shmseg
)
231 struct shm_handle
*shm_handle
;
234 shm_handle
= CAST_DOWN(void *,shmseg
->shm_internal
); /* tunnel */
235 size
= mach_vm_round_page(shmseg
->shm_segsz
);
236 mach_memory_entry_port_release(shm_handle
->shm_object
);
237 shm_handle
->shm_object
= NULL
;
238 FREE((caddr_t
)shm_handle
, M_SHM
);
239 shmseg
->shm_internal
= USER_ADDR_NULL
; /* tunnel */
240 shm_committed
-= btoc(size
);
242 shmseg
->shm_perm
.mode
= SHMSEG_FREE
;
246 shm_delete_mapping(__unused
struct proc
*p
, struct shmmap_state
*shmmap_s
,
249 struct user_shmid_ds
*shmseg
;
253 segnum
= IPCID_TO_IX(shmmap_s
->shmid
);
254 shmseg
= &shmsegs
[segnum
];
255 size
= mach_vm_round_page(shmseg
->shm_segsz
); /* XXX done for us? */
257 result
= mach_vm_deallocate(current_map(), shmmap_s
->va
, size
);
258 if (result
!= KERN_SUCCESS
)
261 shmmap_s
->shmid
= -1;
262 shmseg
->shm_dtime
= sysv_shmtime();
263 if ((--shmseg
->shm_nattch
<= 0) &&
264 (shmseg
->shm_perm
.mode
& SHMSEG_REMOVED
)) {
265 shm_deallocate_segment(shmseg
);
266 shm_last_free
= segnum
;
272 shmdt(struct proc
*p
, struct shmdt_args
*uap
, register_t
*retval
)
274 struct shmmap_state
*shmmap_s
;
278 // LP64todo - fix this
279 AUDIT_ARG(svipc_addr
, CAST_DOWN(void *,uap
->shmaddr
));
281 SYSV_SHM_SUBSYS_LOCK();
287 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
288 if (shmmap_s
== NULL
) {
293 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
294 if (shmmap_s
->shmid
!= -1 &&
295 shmmap_s
->va
== (mach_vm_offset_t
)uap
->shmaddr
)
297 if (i
== shminfo
.shmseg
) {
301 i
= shm_delete_mapping(p
, shmmap_s
, 1);
307 SYSV_SHM_SUBSYS_UNLOCK();
312 shmat(struct proc
*p
, struct shmat_args
*uap
, register_t
*retval
)
315 struct user_shmid_ds
*shmseg
;
316 struct shmmap_state
*shmmap_s
= NULL
;
317 struct shm_handle
*shm_handle
;
318 mach_vm_address_t attach_va
; /* attach address in/out */
319 mach_vm_size_t map_size
; /* size of map entry */
325 AUDIT_ARG(svipc_id
, uap
->shmid
);
326 // LP64todo - fix this
327 AUDIT_ARG(svipc_addr
, CAST_DOWN(void *,uap
->shmaddr
));
329 SYSV_SHM_SUBSYS_LOCK();
336 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
338 if (shmmap_s
== NULL
) {
339 size
= shminfo
.shmseg
* sizeof(struct shmmap_state
);
340 MALLOC(shmmap_s
, struct shmmap_state
*, size
, M_SHM
, M_WAITOK
);
341 if (shmmap_s
== NULL
) {
345 for (i
= 0; i
< shminfo
.shmseg
; i
++)
346 shmmap_s
[i
].shmid
= -1;
347 p
->vm_shm
= (caddr_t
)shmmap_s
;
349 shmseg
= shm_find_segment_by_shmid(uap
->shmid
);
350 if (shmseg
== NULL
) {
355 AUDIT_ARG(svipc_perm
, &shmseg
->shm_perm
);
356 error
= ipcperm(kauth_cred_get(), &shmseg
->shm_perm
,
357 (uap
->shmflg
& SHM_RDONLY
) ? IPC_R
: IPC_R
|IPC_W
);
363 for (i
= 0; i
< shminfo
.shmseg
; i
++) {
364 if (shmmap_s
->shmid
== -1)
368 if (i
>= shminfo
.shmseg
) {
373 map_size
= mach_vm_round_page(shmseg
->shm_segsz
);
375 if ((uap
->shmflg
& SHM_RDONLY
) == 0)
376 prot
|= VM_PROT_WRITE
;
377 flags
= MAP_ANON
| MAP_SHARED
;
381 attach_va
= (mach_vm_address_t
)uap
->shmaddr
;
382 if (uap
->shmflg
& SHM_RND
)
383 attach_va
&= ~(SHMLBA
-1);
384 else if ((attach_va
& (SHMLBA
-1)) != 0) {
389 shm_handle
= CAST_DOWN(void *, shmseg
->shm_internal
); /* tunnel */
391 rv
= mach_vm_map(current_map(), /* process map */
392 &attach_va
, /* attach address */
393 map_size
, /* segment size */
394 (mach_vm_offset_t
)0, /* alignment mask */
395 (flags
& MAP_FIXED
)? VM_FLAGS_FIXED
: VM_FLAGS_ANYWHERE
,
396 shm_handle
->shm_object
,
402 if (rv
!= KERN_SUCCESS
)
405 rv
= mach_vm_inherit(current_map(), attach_va
, map_size
, VM_INHERIT_SHARE
);
406 if (rv
!= KERN_SUCCESS
) {
407 (void)mach_vm_deallocate(current_map(), attach_va
, map_size
);
411 shmmap_s
->va
= attach_va
;
412 shmmap_s
->shmid
= uap
->shmid
;
413 shmseg
->shm_lpid
= p
->p_pid
;
414 shmseg
->shm_atime
= sysv_shmtime();
415 shmseg
->shm_nattch
++;
416 *retval
= attach_va
; /* XXX return -1 on error */
421 case KERN_INVALID_ADDRESS
:
424 case KERN_PROTECTION_FAILURE
:
430 SYSV_SHM_SUBSYS_UNLOCK();
435 oshmctl(__unused
void *p
, __unused
void *uap
, __unused
void *retval
)
441 shmctl(__unused
struct proc
*p
, struct shmctl_args
*uap
, register_t
*retval
)
444 kauth_cred_t cred
= kauth_cred_get();
445 struct user_shmid_ds inbuf
;
446 struct user_shmid_ds
*shmseg
;
447 size_t shmid_ds_sz
= sizeof(struct user_shmid_ds
);
451 AUDIT_ARG(svipc_cmd
, uap
->cmd
);
452 AUDIT_ARG(svipc_id
, uap
->shmid
);
454 SYSV_SHM_SUBSYS_LOCK();
461 if (!IS_64BIT_PROCESS(p
))
462 shmid_ds_sz
= sizeof(struct shmid_ds
);
464 shmseg
= shm_find_segment_by_shmid(uap
->shmid
);
465 if (shmseg
== NULL
) {
470 /* XXAUDIT: This is the perms BEFORE any change by this call. This
471 * may not be what is desired.
473 AUDIT_ARG(svipc_perm
, &shmseg
->shm_perm
);
477 error
= ipcperm(cred
, &shmseg
->shm_perm
, IPC_R
);
483 if (IS_64BIT_PROCESS(p
)) {
484 error
= copyout(shmseg
, uap
->buf
, sizeof(struct user_shmid_ds
));
486 struct shmid_ds shmid_ds32
;
487 shmid_ds_64to32(shmseg
, &shmid_ds32
);
488 error
= copyout(&shmid_ds32
, uap
->buf
, sizeof(struct shmid_ds
));
496 error
= ipcperm(cred
, &shmseg
->shm_perm
, IPC_M
);
501 if (IS_64BIT_PROCESS(p
)) {
502 error
= copyin(uap
->buf
, &inbuf
, sizeof(struct user_shmid_ds
));
504 error
= copyin(uap
->buf
, &inbuf
, sizeof(struct shmid_ds
));
505 /* convert in place; ugly, but safe */
506 shmid_ds_32to64((struct shmid_ds
*)&inbuf
, &inbuf
);
512 shmseg
->shm_perm
.uid
= inbuf
.shm_perm
.uid
;
513 shmseg
->shm_perm
.gid
= inbuf
.shm_perm
.gid
;
514 shmseg
->shm_perm
.mode
=
515 (shmseg
->shm_perm
.mode
& ~ACCESSPERMS
) |
516 (inbuf
.shm_perm
.mode
& ACCESSPERMS
);
517 shmseg
->shm_ctime
= sysv_shmtime();
520 error
= ipcperm(cred
, &shmseg
->shm_perm
, IPC_M
);
525 shmseg
->shm_perm
.key
= IPC_PRIVATE
;
526 shmseg
->shm_perm
.mode
|= SHMSEG_REMOVED
;
527 if (shmseg
->shm_nattch
<= 0) {
528 shm_deallocate_segment(shmseg
);
529 shm_last_free
= IPCID_TO_IX(uap
->shmid
);
543 SYSV_SHM_SUBSYS_UNLOCK();
548 shmget_existing(struct shmget_args
*uap
, int mode
, int segnum
, int *retval
)
550 struct user_shmid_ds
*shmseg
;
553 shmseg
= &shmsegs
[segnum
];
554 if (shmseg
->shm_perm
.mode
& SHMSEG_REMOVED
) {
556 * This segment is in the process of being allocated. Wait
557 * until it's done, and look the key up again (in case the
558 * allocation failed or it was freed).
560 shmseg
->shm_perm
.mode
|= SHMSEG_WANTED
;
561 error
= tsleep((caddr_t
)shmseg
, PLOCK
| PCATCH
, "shmget", 0);
566 error
= ipcperm(kauth_cred_get(), &shmseg
->shm_perm
, mode
);
569 if (uap
->size
&& uap
->size
> shmseg
->shm_segsz
)
571 if ((uap
->shmflg
& (IPC_CREAT
| IPC_EXCL
)) == (IPC_CREAT
| IPC_EXCL
))
573 *retval
= IXSEQ_TO_IPCID(segnum
, shmseg
->shm_perm
);
578 shmget_allocate_segment(struct proc
*p
, struct shmget_args
*uap
, int mode
,
581 int i
, segnum
, shmid
, size
;
582 kauth_cred_t cred
= kauth_cred_get();
583 struct user_shmid_ds
*shmseg
;
584 struct shm_handle
*shm_handle
;
586 vm_offset_t user_addr
;
589 if (uap
->size
< (user_size_t
)shminfo
.shmmin
||
590 uap
->size
> (user_size_t
)shminfo
.shmmax
)
592 if (shm_nused
>= shminfo
.shmmni
) /* any shmids left? */
594 size
= mach_vm_round_page(uap
->size
);
595 if (shm_committed
+ btoc(size
) > shminfo
.shmall
)
597 if (shm_last_free
< 0) {
598 for (i
= 0; i
< shminfo
.shmmni
; i
++)
599 if (shmsegs
[i
].shm_perm
.mode
& SHMSEG_FREE
)
601 if (i
== shminfo
.shmmni
)
602 panic("shmseg free count inconsistent");
605 segnum
= shm_last_free
;
608 shmseg
= &shmsegs
[segnum
];
610 * In case we sleep in malloc(), mark the segment present but deleted
611 * so that noone else tries to create the same key.
613 kret
= vm_allocate(current_map(), &user_addr
, size
, VM_FLAGS_ANYWHERE
);
614 if (kret
!= KERN_SUCCESS
)
617 kret
= mach_make_memory_entry (current_map(), &size
, user_addr
,
618 VM_PROT_DEFAULT
, (mem_entry_name_port_t
*)&mem_object
, 0);
620 if (kret
!= KERN_SUCCESS
)
623 vm_deallocate(current_map(), user_addr
, size
);
625 shmseg
->shm_perm
.mode
= SHMSEG_ALLOCATED
| SHMSEG_REMOVED
;
626 shmseg
->shm_perm
.key
= uap
->key
;
627 shmseg
->shm_perm
.seq
= (shmseg
->shm_perm
.seq
+ 1) & 0x7fff;
628 MALLOC(shm_handle
, struct shm_handle
*, sizeof(struct shm_handle
), M_SHM
, M_WAITOK
);
629 if (shm_handle
== NULL
) {
630 kret
= KERN_NO_SPACE
;
631 mach_memory_entry_port_release(mem_object
);
635 shm_handle
->shm_object
= mem_object
;
636 shmid
= IXSEQ_TO_IPCID(segnum
, shmseg
->shm_perm
);
638 shmseg
->shm_internal
= CAST_USER_ADDR_T(shm_handle
); /* tunnel */
639 shmseg
->shm_perm
.cuid
= shmseg
->shm_perm
.uid
= kauth_cred_getuid(cred
);
640 shmseg
->shm_perm
.cgid
= shmseg
->shm_perm
.gid
= cred
->cr_gid
;
641 shmseg
->shm_perm
.mode
= (shmseg
->shm_perm
.mode
& SHMSEG_WANTED
) |
642 (mode
& ACCESSPERMS
) | SHMSEG_ALLOCATED
;
643 shmseg
->shm_segsz
= uap
->size
;
644 shmseg
->shm_cpid
= p
->p_pid
;
645 shmseg
->shm_lpid
= shmseg
->shm_nattch
= 0;
646 shmseg
->shm_atime
= shmseg
->shm_dtime
= 0;
647 shmseg
->shm_ctime
= sysv_shmtime();
648 shm_committed
+= btoc(size
);
650 AUDIT_ARG(svipc_perm
, &shmseg
->shm_perm
);
651 if (shmseg
->shm_perm
.mode
& SHMSEG_WANTED
) {
653 * Somebody else wanted this key while we were asleep. Wake
656 shmseg
->shm_perm
.mode
&= ~SHMSEG_WANTED
;
657 wakeup((caddr_t
)shmseg
);
660 AUDIT_ARG(svipc_id
, shmid
);
664 case KERN_INVALID_ADDRESS
:
667 case KERN_PROTECTION_FAILURE
:
676 shmget(struct proc
*p
, struct shmget_args
*uap
, register_t
*retval
)
678 int segnum
, mode
, error
;
681 /* Auditing is actually done in shmget_allocate_segment() */
683 SYSV_SHM_SUBSYS_LOCK();
690 mode
= uap
->shmflg
& ACCESSPERMS
;
691 if (uap
->key
!= IPC_PRIVATE
) {
693 segnum
= shm_find_segment_by_key(uap
->key
);
695 error
= shmget_existing(uap
, mode
, segnum
, retval
);
701 if ((uap
->shmflg
& IPC_CREAT
) == 0) {
706 shmget_ret
= shmget_allocate_segment(p
, uap
, mode
, retval
);
708 SYSV_SHM_SUBSYS_UNLOCK();
714 /* XXX actually varargs. */
716 shmsys(struct proc
*p
, struct shmsys_args
*uap
, register_t
*retval
)
719 /* The routine that we are dispatching already does this */
721 if (uap
->which
>= sizeof(shmcalls
)/sizeof(shmcalls
[0]))
723 return ((*shmcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
727 * Return 0 on success, 1 on failure.
730 shmfork(struct proc
*p1
, struct proc
*p2
)
732 struct shmmap_state
*shmmap_s
;
737 SYSV_SHM_SUBSYS_LOCK();
744 size
= shminfo
.shmseg
* sizeof(struct shmmap_state
);
745 MALLOC(shmmap_s
, struct shmmap_state
*, size
, M_SHM
, M_WAITOK
);
746 if (shmmap_s
!= NULL
) {
747 bcopy((caddr_t
)p1
->vm_shm
, (caddr_t
)shmmap_s
, size
);
748 p2
->vm_shm
= (caddr_t
)shmmap_s
;
749 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
750 if (shmmap_s
->shmid
!= -1)
751 shmsegs
[IPCID_TO_IX(shmmap_s
->shmid
)].shm_nattch
++;
756 shmfork_ret
= 1; /* failed to copy to child - ENOMEM */
758 SYSV_SHM_SUBSYS_UNLOCK();
763 shmexit(struct proc
*p
)
765 struct shmmap_state
*shmmap_s
;
768 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
770 SYSV_SHM_SUBSYS_LOCK();
771 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
772 if (shmmap_s
->shmid
!= -1)
773 shm_delete_mapping(p
, shmmap_s
, 1);
774 FREE((caddr_t
)p
->vm_shm
, M_SHM
);
776 SYSV_SHM_SUBSYS_UNLOCK();
780 * shmexec() is like shmexit(), only it doesn't delete the mappings,
781 * since the old address space has already been destroyed and the new
782 * one instantiated. Instead, it just does the housekeeping work we
783 * need to do to keep the System V shared memory subsystem sane.
785 __private_extern__
void
786 shmexec(struct proc
*p
)
788 struct shmmap_state
*shmmap_s
;
791 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
792 SYSV_SHM_SUBSYS_LOCK();
793 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
794 if (shmmap_s
->shmid
!= -1)
795 shm_delete_mapping(p
, shmmap_s
, 0);
796 FREE((caddr_t
)p
->vm_shm
, M_SHM
);
798 SYSV_SHM_SUBSYS_UNLOCK();
802 shminit(__unused
void *dummy
)
809 * we store internally 64 bit, since if we didn't, we would
810 * be unable to represent a segment size in excess of 32 bits
811 * with the (struct shmid_ds)->shm_segsz field; also, POSIX
812 * dictates this filed be a size_t, which is 64 bits when
813 * running 64 bit binaries.
815 s
= sizeof(struct user_shmid_ds
) * shminfo
.shmmni
;
817 MALLOC(shmsegs
, struct user_shmid_ds
*, s
, M_SHM
, M_WAITOK
);
818 if (shmsegs
== NULL
) {
819 /* XXX fail safely: leave shared memory uninited */
822 for (i
= 0; i
< shminfo
.shmmni
; i
++) {
823 shmsegs
[i
].shm_perm
.mode
= SHMSEG_FREE
;
824 shmsegs
[i
].shm_perm
.seq
= 0;
832 /* Initialize the mutex governing access to the SysV shm subsystem */
833 __private_extern__
void
834 sysv_shm_lock_init( void )
837 sysv_shm_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
839 sysv_shm_subsys_lck_grp
= lck_grp_alloc_init("sysv_shm_subsys_lock", sysv_shm_subsys_lck_grp_attr
);
841 sysv_shm_subsys_lck_attr
= lck_attr_alloc_init();
842 lck_mtx_init(&sysv_shm_subsys_mutex
, sysv_shm_subsys_lck_grp
, sysv_shm_subsys_lck_attr
);
845 /* (struct sysctl_oid *oidp, void *arg1, int arg2, \
846 struct sysctl_req *req) */
848 sysctl_shminfo(__unused
struct sysctl_oid
*oidp
, void *arg1
,
849 __unused
int arg2
, struct sysctl_req
*req
)
852 int sysctl_shminfo_ret
= 0;
854 error
= SYSCTL_OUT(req
, arg1
, sizeof(int64_t));
855 if (error
|| req
->newptr
== USER_ADDR_NULL
)
858 SYSV_SHM_SUBSYS_LOCK();
859 /* Set the values only if shared memory is not initialised */
861 if ((error
= SYSCTL_IN(req
, arg1
, sizeof(int64_t)))
863 sysctl_shminfo_ret
= error
;
864 goto sysctl_shminfo_out
;
867 if (arg1
== &shminfo
.shmmax
) {
868 if (shminfo
.shmmax
& PAGE_MASK_64
) {
869 shminfo
.shmmax
= (int64_t)-1;
870 sysctl_shminfo_ret
= EINVAL
;
871 goto sysctl_shminfo_out
;
875 /* Initialize only when all values are set */
876 if ((shminfo
.shmmax
!= (int64_t)-1) &&
877 (shminfo
.shmmin
!= (int64_t)-1) &&
878 (shminfo
.shmmni
!= (int64_t)-1) &&
879 (shminfo
.shmseg
!= (int64_t)-1) &&
880 (shminfo
.shmall
!= (int64_t)-1)) {
884 sysctl_shminfo_ret
= 0;
886 SYSV_SHM_SUBSYS_UNLOCK();
887 return sysctl_shminfo_ret
;
891 IPCS_shm_sysctl(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
892 __unused
int arg2
, struct sysctl_req
*req
)
897 struct IPCS_command u32
;
898 struct user_IPCS_command u64
;
900 struct shmid_ds shmid_ds32
; /* post conversion, 32 bit version */
902 size_t ipcs_sz
= sizeof(struct user_IPCS_command
);
903 size_t shmid_ds_sz
= sizeof(struct user_shmid_ds
);
904 struct proc
*p
= current_proc();
906 int ipcs__shminfo_ret
= 0;
908 SYSV_SHM_SUBSYS_LOCK();
912 goto ipcs_shm_sysctl_out
;
915 if (!IS_64BIT_PROCESS(p
)) {
916 ipcs_sz
= sizeof(struct IPCS_command
);
917 shmid_ds_sz
= sizeof(struct shmid_ds
);
920 /* Copy in the command structure */
921 if ((error
= SYSCTL_IN(req
, &ipcs
, ipcs_sz
)) != 0) {
922 goto ipcs_shm_sysctl_out
;
925 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
926 ipcs
.u64
.ipcs_data
= CAST_USER_ADDR_T(ipcs
.u32
.ipcs_data
);
928 /* Let us version this interface... */
929 if (ipcs
.u64
.ipcs_magic
!= IPCS_MAGIC
) {
931 goto ipcs_shm_sysctl_out
;
934 switch(ipcs
.u64
.ipcs_op
) {
935 case IPCS_SHM_CONF
: /* Obtain global configuration data */
936 if (ipcs
.u64
.ipcs_datalen
!= sizeof(struct shminfo
)) {
937 if (ipcs
.u64
.ipcs_cursor
!= 0) { /* fwd. compat. */
944 error
= copyout(&shminfo
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
947 case IPCS_SHM_ITER
: /* Iterate over existing segments */
948 cursor
= ipcs
.u64
.ipcs_cursor
;
949 if (cursor
< 0 || cursor
>= shminfo
.shmmni
) {
953 if (ipcs
.u64
.ipcs_datalen
!= (int)shmid_ds_sz
) {
957 for( ; cursor
< shminfo
.shmmni
; cursor
++) {
958 if (shmsegs
[cursor
].shm_perm
.mode
& SHMSEG_ALLOCATED
)
962 if (cursor
== shminfo
.shmmni
) {
967 shmid_dsp
= &shmsegs
[cursor
]; /* default: 64 bit */
970 * If necessary, convert the 64 bit kernel segment
971 * descriptor to a 32 bit user one.
973 if (!IS_64BIT_PROCESS(p
)) {
974 shmid_ds_64to32(shmid_dsp
, &shmid_ds32
);
975 shmid_dsp
= &shmid_ds32
;
977 error
= copyout(shmid_dsp
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
980 ipcs
.u64
.ipcs_cursor
= cursor
+ 1;
982 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
983 ipcs
.u32
.ipcs_data
= CAST_DOWN(void *,ipcs
.u64
.ipcs_data
);
984 error
= SYSCTL_OUT(req
, &ipcs
, ipcs_sz
);
993 SYSV_SHM_SUBSYS_UNLOCK();
997 SYSCTL_NODE(_kern
, KERN_SYSV
, sysv
, CTLFLAG_RW
, 0, "SYSV");
999 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMMAX
, shmmax
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1000 &shminfo
.shmmax
, 0, &sysctl_shminfo
,"Q","shmmax");
1002 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMMIN
, shmmin
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1003 &shminfo
.shmmin
, 0, &sysctl_shminfo
,"Q","shmmin");
1005 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMMNI
, shmmni
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1006 &shminfo
.shmmni
, 0, &sysctl_shminfo
,"Q","shmmni");
1008 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMSEG
, shmseg
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1009 &shminfo
.shmseg
, 0, &sysctl_shminfo
,"Q","shmseg");
1011 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMALL
, shmall
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1012 &shminfo
.shmall
, 0, &sysctl_shminfo
,"Q","shmall");
1014 SYSCTL_NODE(_kern_sysv
, OID_AUTO
, ipcs
, CTLFLAG_RW
, 0, "SYSVIPCS");
1016 SYSCTL_PROC(_kern_sysv_ipcs
, OID_AUTO
, shm
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
1017 0, 0, IPCS_shm_sysctl
,
1018 "S,IPCS_shm_command",
1019 "ipcs shm command interface");