2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
33 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by Adam Glass and Charles
47 * 4. The names of the authors may not be used to endorse or promote products
48 * derived from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 #include <sys/appleapiopts.h>
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/shm_internal.h>
68 #include <sys/proc_internal.h>
69 #include <sys/kauth.h>
70 #include <sys/malloc.h>
73 #include <sys/sysctl.h>
75 #include <sys/sysent.h>
76 #include <sys/sysproto.h>
78 #include <bsm/audit_kernel.h>
80 #include <mach/mach_types.h>
81 #include <mach/vm_inherit.h>
82 #include <mach/vm_map.h>
84 #include <mach/mach_vm.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_shared_memory_server.h>
88 #include <vm/vm_protos.h>
90 #include <kern/locks.h>
92 static void shminit(void *);
94 SYSINIT(sysv_shm
, SI_SUB_SYSV_SHM
, SI_ORDER_FIRST
, shminit
, NULL
)
97 static lck_grp_t
*sysv_shm_subsys_lck_grp
;
98 static lck_grp_attr_t
*sysv_shm_subsys_lck_grp_attr
;
99 static lck_attr_t
*sysv_shm_subsys_lck_attr
;
100 static lck_mtx_t sysv_shm_subsys_mutex
;
102 #define SYSV_SHM_SUBSYS_LOCK() lck_mtx_lock(&sysv_shm_subsys_mutex)
103 #define SYSV_SHM_SUBSYS_UNLOCK() lck_mtx_unlock(&sysv_shm_subsys_mutex)
105 static int oshmctl(void *p
, void *uap
, void *retval
);
106 static int shmget_allocate_segment(struct proc
*p
, struct shmget_args
*uap
, int mode
, int * retval
);
107 static int shmget_existing(struct shmget_args
*uap
, int mode
, int segnum
, int * retval
);
108 static void shmid_ds_64to32(struct user_shmid_ds
*in
, struct shmid_ds
*out
);
109 static void shmid_ds_32to64(struct shmid_ds
*in
, struct user_shmid_ds
*out
);
111 /* XXX casting to (sy_call_t *) is bogus, as usual. */
112 static sy_call_t
*shmcalls
[] = {
113 (sy_call_t
*)shmat
, (sy_call_t
*)oshmctl
,
114 (sy_call_t
*)shmdt
, (sy_call_t
*)shmget
,
118 #define SHMSEG_FREE 0x0200
119 #define SHMSEG_REMOVED 0x0400
120 #define SHMSEG_ALLOCATED 0x0800
121 #define SHMSEG_WANTED 0x1000
123 static int shm_last_free
, shm_nused
, shm_committed
;
124 struct user_shmid_ds
*shmsegs
; /* 64 bit version */
125 static int shm_inited
= 0;
128 void * shm_object
; /* vm_offset_t kva; */
131 struct shmmap_state
{
132 mach_vm_address_t va
; /* user address */
133 int shmid
; /* segment id */
136 static void shm_deallocate_segment(struct user_shmid_ds
*);
137 static int shm_find_segment_by_key(key_t
);
138 static struct user_shmid_ds
*shm_find_segment_by_shmid(int);
139 static int shm_delete_mapping(struct proc
*, struct shmmap_state
*, int);
141 #ifdef __APPLE_API_PRIVATE
142 struct shminfo shminfo
= {
143 -1, /* SHMMAX 4096 *1024 */
147 -1 /* SHMALL = 1024 */
149 #endif /* __APPLE_API_PRIVATE */
151 void sysv_shm_lock_init(void);
153 static __inline__
time_t
162 * This conversion is safe, since if we are converting for a 32 bit process,
163 * then it's value of (struct shmid_ds)->shm_segsz will never exceed 4G.
165 * NOTE: Source and target may *NOT* overlap! (target is smaller)
168 shmid_ds_64to32(struct user_shmid_ds
*in
, struct shmid_ds
*out
)
170 out
->shm_perm
= in
->shm_perm
;
171 out
->shm_segsz
= (size_t)in
->shm_segsz
;
172 out
->shm_lpid
= in
->shm_lpid
;
173 out
->shm_cpid
= in
->shm_cpid
;
174 out
->shm_nattch
= in
->shm_nattch
;
175 out
->shm_atime
= in
->shm_atime
;
176 out
->shm_dtime
= in
->shm_dtime
;
177 out
->shm_ctime
= in
->shm_ctime
;
178 out
->shm_internal
= CAST_DOWN(void *,in
->shm_internal
);
182 * NOTE: Source and target may are permitted to overlap! (source is smaller);
183 * this works because we copy fields in order from the end of the struct to
187 shmid_ds_32to64(struct shmid_ds
*in
, struct user_shmid_ds
*out
)
189 out
->shm_internal
= CAST_USER_ADDR_T(in
->shm_internal
);
190 out
->shm_ctime
= in
->shm_ctime
;
191 out
->shm_dtime
= in
->shm_dtime
;
192 out
->shm_atime
= in
->shm_atime
;
193 out
->shm_nattch
= in
->shm_nattch
;
194 out
->shm_cpid
= in
->shm_cpid
;
195 out
->shm_lpid
= in
->shm_lpid
;
196 out
->shm_segsz
= (user_size_t
)in
->shm_segsz
;
197 out
->shm_perm
= in
->shm_perm
;
202 shm_find_segment_by_key(key_t key
)
206 for (i
= 0; i
< shminfo
.shmmni
; i
++)
207 if ((shmsegs
[i
].shm_perm
.mode
& SHMSEG_ALLOCATED
) &&
208 shmsegs
[i
].shm_perm
.key
== key
)
213 static struct user_shmid_ds
*
214 shm_find_segment_by_shmid(int shmid
)
217 struct user_shmid_ds
*shmseg
;
219 segnum
= IPCID_TO_IX(shmid
);
220 if (segnum
< 0 || segnum
>= shminfo
.shmmni
)
222 shmseg
= &shmsegs
[segnum
];
223 if ((shmseg
->shm_perm
.mode
& (SHMSEG_ALLOCATED
| SHMSEG_REMOVED
))
224 != SHMSEG_ALLOCATED
||
225 shmseg
->shm_perm
.seq
!= IPCID_TO_SEQ(shmid
))
231 shm_deallocate_segment(struct user_shmid_ds
*shmseg
)
233 struct shm_handle
*shm_handle
;
236 shm_handle
= CAST_DOWN(void *,shmseg
->shm_internal
); /* tunnel */
237 size
= mach_vm_round_page(shmseg
->shm_segsz
);
238 mach_memory_entry_port_release(shm_handle
->shm_object
);
239 shm_handle
->shm_object
= NULL
;
240 FREE((caddr_t
)shm_handle
, M_SHM
);
241 shmseg
->shm_internal
= USER_ADDR_NULL
; /* tunnel */
242 shm_committed
-= btoc(size
);
244 shmseg
->shm_perm
.mode
= SHMSEG_FREE
;
248 shm_delete_mapping(__unused
struct proc
*p
, struct shmmap_state
*shmmap_s
,
251 struct user_shmid_ds
*shmseg
;
255 segnum
= IPCID_TO_IX(shmmap_s
->shmid
);
256 shmseg
= &shmsegs
[segnum
];
257 size
= mach_vm_round_page(shmseg
->shm_segsz
); /* XXX done for us? */
259 result
= mach_vm_deallocate(current_map(), shmmap_s
->va
, size
);
260 if (result
!= KERN_SUCCESS
)
263 shmmap_s
->shmid
= -1;
264 shmseg
->shm_dtime
= sysv_shmtime();
265 if ((--shmseg
->shm_nattch
<= 0) &&
266 (shmseg
->shm_perm
.mode
& SHMSEG_REMOVED
)) {
267 shm_deallocate_segment(shmseg
);
268 shm_last_free
= segnum
;
274 shmdt(struct proc
*p
, struct shmdt_args
*uap
, register_t
*retval
)
276 struct shmmap_state
*shmmap_s
;
280 // LP64todo - fix this
281 AUDIT_ARG(svipc_addr
, CAST_DOWN(void *,uap
->shmaddr
));
283 SYSV_SHM_SUBSYS_LOCK();
289 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
290 if (shmmap_s
== NULL
) {
295 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
296 if (shmmap_s
->shmid
!= -1 &&
297 shmmap_s
->va
== (mach_vm_offset_t
)uap
->shmaddr
)
299 if (i
== shminfo
.shmseg
) {
303 i
= shm_delete_mapping(p
, shmmap_s
, 1);
309 SYSV_SHM_SUBSYS_UNLOCK();
314 shmat(struct proc
*p
, struct shmat_args
*uap
, register_t
*retval
)
317 struct user_shmid_ds
*shmseg
;
318 struct shmmap_state
*shmmap_s
= NULL
;
319 struct shm_handle
*shm_handle
;
320 mach_vm_address_t attach_va
; /* attach address in/out */
321 mach_vm_size_t map_size
; /* size of map entry */
327 AUDIT_ARG(svipc_id
, uap
->shmid
);
328 // LP64todo - fix this
329 AUDIT_ARG(svipc_addr
, CAST_DOWN(void *,uap
->shmaddr
));
331 SYSV_SHM_SUBSYS_LOCK();
338 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
340 if (shmmap_s
== NULL
) {
341 size
= shminfo
.shmseg
* sizeof(struct shmmap_state
);
342 MALLOC(shmmap_s
, struct shmmap_state
*, size
, M_SHM
, M_WAITOK
);
343 if (shmmap_s
== NULL
) {
347 for (i
= 0; i
< shminfo
.shmseg
; i
++)
348 shmmap_s
[i
].shmid
= -1;
349 p
->vm_shm
= (caddr_t
)shmmap_s
;
351 shmseg
= shm_find_segment_by_shmid(uap
->shmid
);
352 if (shmseg
== NULL
) {
357 AUDIT_ARG(svipc_perm
, &shmseg
->shm_perm
);
358 error
= ipcperm(kauth_cred_get(), &shmseg
->shm_perm
,
359 (uap
->shmflg
& SHM_RDONLY
) ? IPC_R
: IPC_R
|IPC_W
);
365 for (i
= 0; i
< shminfo
.shmseg
; i
++) {
366 if (shmmap_s
->shmid
== -1)
370 if (i
>= shminfo
.shmseg
) {
375 map_size
= mach_vm_round_page(shmseg
->shm_segsz
);
377 if ((uap
->shmflg
& SHM_RDONLY
) == 0)
378 prot
|= VM_PROT_WRITE
;
379 flags
= MAP_ANON
| MAP_SHARED
;
383 attach_va
= (mach_vm_address_t
)uap
->shmaddr
;
384 if (uap
->shmflg
& SHM_RND
)
385 attach_va
&= ~(SHMLBA
-1);
386 else if ((attach_va
& (SHMLBA
-1)) != 0) {
391 shm_handle
= CAST_DOWN(void *, shmseg
->shm_internal
); /* tunnel */
393 rv
= mach_vm_map(current_map(), /* process map */
394 &attach_va
, /* attach address */
395 map_size
, /* segment size */
396 (mach_vm_offset_t
)0, /* alignment mask */
397 (flags
& MAP_FIXED
)? VM_FLAGS_FIXED
: VM_FLAGS_ANYWHERE
,
398 shm_handle
->shm_object
,
404 if (rv
!= KERN_SUCCESS
)
407 rv
= mach_vm_inherit(current_map(), attach_va
, map_size
, VM_INHERIT_SHARE
);
408 if (rv
!= KERN_SUCCESS
) {
409 (void)mach_vm_deallocate(current_map(), attach_va
, map_size
);
413 shmmap_s
->va
= attach_va
;
414 shmmap_s
->shmid
= uap
->shmid
;
415 shmseg
->shm_lpid
= p
->p_pid
;
416 shmseg
->shm_atime
= sysv_shmtime();
417 shmseg
->shm_nattch
++;
418 *retval
= attach_va
; /* XXX return -1 on error */
423 case KERN_INVALID_ADDRESS
:
426 case KERN_PROTECTION_FAILURE
:
432 SYSV_SHM_SUBSYS_UNLOCK();
437 oshmctl(__unused
void *p
, __unused
void *uap
, __unused
void *retval
)
443 shmctl(__unused
struct proc
*p
, struct shmctl_args
*uap
, register_t
*retval
)
446 kauth_cred_t cred
= kauth_cred_get();
447 struct user_shmid_ds inbuf
;
448 struct user_shmid_ds
*shmseg
;
449 size_t shmid_ds_sz
= sizeof(struct user_shmid_ds
);
453 AUDIT_ARG(svipc_cmd
, uap
->cmd
);
454 AUDIT_ARG(svipc_id
, uap
->shmid
);
456 SYSV_SHM_SUBSYS_LOCK();
463 if (!IS_64BIT_PROCESS(p
))
464 shmid_ds_sz
= sizeof(struct shmid_ds
);
466 shmseg
= shm_find_segment_by_shmid(uap
->shmid
);
467 if (shmseg
== NULL
) {
472 /* XXAUDIT: This is the perms BEFORE any change by this call. This
473 * may not be what is desired.
475 AUDIT_ARG(svipc_perm
, &shmseg
->shm_perm
);
479 error
= ipcperm(cred
, &shmseg
->shm_perm
, IPC_R
);
485 if (IS_64BIT_PROCESS(p
)) {
486 error
= copyout(shmseg
, uap
->buf
, sizeof(struct user_shmid_ds
));
488 struct shmid_ds shmid_ds32
;
489 shmid_ds_64to32(shmseg
, &shmid_ds32
);
490 error
= copyout(&shmid_ds32
, uap
->buf
, sizeof(struct shmid_ds
));
498 error
= ipcperm(cred
, &shmseg
->shm_perm
, IPC_M
);
503 if (IS_64BIT_PROCESS(p
)) {
504 error
= copyin(uap
->buf
, &inbuf
, sizeof(struct user_shmid_ds
));
506 error
= copyin(uap
->buf
, &inbuf
, sizeof(struct shmid_ds
));
507 /* convert in place; ugly, but safe */
508 shmid_ds_32to64((struct shmid_ds
*)&inbuf
, &inbuf
);
514 shmseg
->shm_perm
.uid
= inbuf
.shm_perm
.uid
;
515 shmseg
->shm_perm
.gid
= inbuf
.shm_perm
.gid
;
516 shmseg
->shm_perm
.mode
=
517 (shmseg
->shm_perm
.mode
& ~ACCESSPERMS
) |
518 (inbuf
.shm_perm
.mode
& ACCESSPERMS
);
519 shmseg
->shm_ctime
= sysv_shmtime();
522 error
= ipcperm(cred
, &shmseg
->shm_perm
, IPC_M
);
527 shmseg
->shm_perm
.key
= IPC_PRIVATE
;
528 shmseg
->shm_perm
.mode
|= SHMSEG_REMOVED
;
529 if (shmseg
->shm_nattch
<= 0) {
530 shm_deallocate_segment(shmseg
);
531 shm_last_free
= IPCID_TO_IX(uap
->shmid
);
545 SYSV_SHM_SUBSYS_UNLOCK();
550 shmget_existing(struct shmget_args
*uap
, int mode
, int segnum
, int *retval
)
552 struct user_shmid_ds
*shmseg
;
555 shmseg
= &shmsegs
[segnum
];
556 if (shmseg
->shm_perm
.mode
& SHMSEG_REMOVED
) {
558 * This segment is in the process of being allocated. Wait
559 * until it's done, and look the key up again (in case the
560 * allocation failed or it was freed).
562 shmseg
->shm_perm
.mode
|= SHMSEG_WANTED
;
563 error
= tsleep((caddr_t
)shmseg
, PLOCK
| PCATCH
, "shmget", 0);
568 error
= ipcperm(kauth_cred_get(), &shmseg
->shm_perm
, mode
);
571 if (uap
->size
&& uap
->size
> shmseg
->shm_segsz
)
573 if ((uap
->shmflg
& (IPC_CREAT
| IPC_EXCL
)) == (IPC_CREAT
| IPC_EXCL
))
575 *retval
= IXSEQ_TO_IPCID(segnum
, shmseg
->shm_perm
);
580 shmget_allocate_segment(struct proc
*p
, struct shmget_args
*uap
, int mode
,
583 int i
, segnum
, shmid
, size
;
584 kauth_cred_t cred
= kauth_cred_get();
585 struct user_shmid_ds
*shmseg
;
586 struct shm_handle
*shm_handle
;
588 vm_offset_t user_addr
;
591 if (uap
->size
< (user_size_t
)shminfo
.shmmin
||
592 uap
->size
> (user_size_t
)shminfo
.shmmax
)
594 if (shm_nused
>= shminfo
.shmmni
) /* any shmids left? */
596 size
= mach_vm_round_page(uap
->size
);
597 if (shm_committed
+ btoc(size
) > shminfo
.shmall
)
599 if (shm_last_free
< 0) {
600 for (i
= 0; i
< shminfo
.shmmni
; i
++)
601 if (shmsegs
[i
].shm_perm
.mode
& SHMSEG_FREE
)
603 if (i
== shminfo
.shmmni
)
604 panic("shmseg free count inconsistent");
607 segnum
= shm_last_free
;
610 shmseg
= &shmsegs
[segnum
];
612 * In case we sleep in malloc(), mark the segment present but deleted
613 * so that noone else tries to create the same key.
615 kret
= vm_allocate(current_map(), &user_addr
, size
, VM_FLAGS_ANYWHERE
);
616 if (kret
!= KERN_SUCCESS
)
619 kret
= mach_make_memory_entry (current_map(), &size
, user_addr
,
620 VM_PROT_DEFAULT
, (mem_entry_name_port_t
*)&mem_object
, 0);
622 if (kret
!= KERN_SUCCESS
)
625 vm_deallocate(current_map(), user_addr
, size
);
627 shmseg
->shm_perm
.mode
= SHMSEG_ALLOCATED
| SHMSEG_REMOVED
;
628 shmseg
->shm_perm
.key
= uap
->key
;
629 shmseg
->shm_perm
.seq
= (shmseg
->shm_perm
.seq
+ 1) & 0x7fff;
630 MALLOC(shm_handle
, struct shm_handle
*, sizeof(struct shm_handle
), M_SHM
, M_WAITOK
);
631 if (shm_handle
== NULL
) {
632 kret
= KERN_NO_SPACE
;
633 mach_memory_entry_port_release(mem_object
);
637 shm_handle
->shm_object
= mem_object
;
638 shmid
= IXSEQ_TO_IPCID(segnum
, shmseg
->shm_perm
);
640 shmseg
->shm_internal
= CAST_USER_ADDR_T(shm_handle
); /* tunnel */
641 shmseg
->shm_perm
.cuid
= shmseg
->shm_perm
.uid
= kauth_cred_getuid(cred
);
642 shmseg
->shm_perm
.cgid
= shmseg
->shm_perm
.gid
= cred
->cr_gid
;
643 shmseg
->shm_perm
.mode
= (shmseg
->shm_perm
.mode
& SHMSEG_WANTED
) |
644 (mode
& ACCESSPERMS
) | SHMSEG_ALLOCATED
;
645 shmseg
->shm_segsz
= uap
->size
;
646 shmseg
->shm_cpid
= p
->p_pid
;
647 shmseg
->shm_lpid
= shmseg
->shm_nattch
= 0;
648 shmseg
->shm_atime
= shmseg
->shm_dtime
= 0;
649 shmseg
->shm_ctime
= sysv_shmtime();
650 shm_committed
+= btoc(size
);
652 AUDIT_ARG(svipc_perm
, &shmseg
->shm_perm
);
653 if (shmseg
->shm_perm
.mode
& SHMSEG_WANTED
) {
655 * Somebody else wanted this key while we were asleep. Wake
658 shmseg
->shm_perm
.mode
&= ~SHMSEG_WANTED
;
659 wakeup((caddr_t
)shmseg
);
662 AUDIT_ARG(svipc_id
, shmid
);
666 case KERN_INVALID_ADDRESS
:
669 case KERN_PROTECTION_FAILURE
:
678 shmget(struct proc
*p
, struct shmget_args
*uap
, register_t
*retval
)
680 int segnum
, mode
, error
;
683 /* Auditing is actually done in shmget_allocate_segment() */
685 SYSV_SHM_SUBSYS_LOCK();
692 mode
= uap
->shmflg
& ACCESSPERMS
;
693 if (uap
->key
!= IPC_PRIVATE
) {
695 segnum
= shm_find_segment_by_key(uap
->key
);
697 error
= shmget_existing(uap
, mode
, segnum
, retval
);
703 if ((uap
->shmflg
& IPC_CREAT
) == 0) {
708 shmget_ret
= shmget_allocate_segment(p
, uap
, mode
, retval
);
710 SYSV_SHM_SUBSYS_UNLOCK();
716 /* XXX actually varargs. */
718 shmsys(struct proc
*p
, struct shmsys_args
*uap
, register_t
*retval
)
721 /* The routine that we are dispatching already does this */
723 if (uap
->which
>= sizeof(shmcalls
)/sizeof(shmcalls
[0]))
725 return ((*shmcalls
[uap
->which
])(p
, &uap
->a2
, retval
));
729 * Return 0 on success, 1 on failure.
732 shmfork(struct proc
*p1
, struct proc
*p2
)
734 struct shmmap_state
*shmmap_s
;
739 SYSV_SHM_SUBSYS_LOCK();
746 size
= shminfo
.shmseg
* sizeof(struct shmmap_state
);
747 MALLOC(shmmap_s
, struct shmmap_state
*, size
, M_SHM
, M_WAITOK
);
748 if (shmmap_s
!= NULL
) {
749 bcopy((caddr_t
)p1
->vm_shm
, (caddr_t
)shmmap_s
, size
);
750 p2
->vm_shm
= (caddr_t
)shmmap_s
;
751 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
752 if (shmmap_s
->shmid
!= -1)
753 shmsegs
[IPCID_TO_IX(shmmap_s
->shmid
)].shm_nattch
++;
758 shmfork_ret
= 1; /* failed to copy to child - ENOMEM */
760 SYSV_SHM_SUBSYS_UNLOCK();
765 shmexit(struct proc
*p
)
767 struct shmmap_state
*shmmap_s
;
770 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
772 SYSV_SHM_SUBSYS_LOCK();
773 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
774 if (shmmap_s
->shmid
!= -1)
775 shm_delete_mapping(p
, shmmap_s
, 1);
776 FREE((caddr_t
)p
->vm_shm
, M_SHM
);
778 SYSV_SHM_SUBSYS_UNLOCK();
782 * shmexec() is like shmexit(), only it doesn't delete the mappings,
783 * since the old address space has already been destroyed and the new
784 * one instantiated. Instead, it just does the housekeeping work we
785 * need to do to keep the System V shared memory subsystem sane.
787 __private_extern__
void
788 shmexec(struct proc
*p
)
790 struct shmmap_state
*shmmap_s
;
793 shmmap_s
= (struct shmmap_state
*)p
->vm_shm
;
794 SYSV_SHM_SUBSYS_LOCK();
795 for (i
= 0; i
< shminfo
.shmseg
; i
++, shmmap_s
++)
796 if (shmmap_s
->shmid
!= -1)
797 shm_delete_mapping(p
, shmmap_s
, 0);
798 FREE((caddr_t
)p
->vm_shm
, M_SHM
);
800 SYSV_SHM_SUBSYS_UNLOCK();
804 shminit(__unused
void *dummy
)
811 * we store internally 64 bit, since if we didn't, we would
812 * be unable to represent a segment size in excess of 32 bits
813 * with the (struct shmid_ds)->shm_segsz field; also, POSIX
814 * dictates this filed be a size_t, which is 64 bits when
815 * running 64 bit binaries.
817 s
= sizeof(struct user_shmid_ds
) * shminfo
.shmmni
;
819 MALLOC(shmsegs
, struct user_shmid_ds
*, s
, M_SHM
, M_WAITOK
);
820 if (shmsegs
== NULL
) {
821 /* XXX fail safely: leave shared memory uninited */
824 for (i
= 0; i
< shminfo
.shmmni
; i
++) {
825 shmsegs
[i
].shm_perm
.mode
= SHMSEG_FREE
;
826 shmsegs
[i
].shm_perm
.seq
= 0;
834 /* Initialize the mutex governing access to the SysV shm subsystem */
835 __private_extern__
void
836 sysv_shm_lock_init( void )
839 sysv_shm_subsys_lck_grp_attr
= lck_grp_attr_alloc_init();
841 sysv_shm_subsys_lck_grp
= lck_grp_alloc_init("sysv_shm_subsys_lock", sysv_shm_subsys_lck_grp_attr
);
843 sysv_shm_subsys_lck_attr
= lck_attr_alloc_init();
844 lck_mtx_init(&sysv_shm_subsys_mutex
, sysv_shm_subsys_lck_grp
, sysv_shm_subsys_lck_attr
);
847 /* (struct sysctl_oid *oidp, void *arg1, int arg2, \
848 struct sysctl_req *req) */
850 sysctl_shminfo(__unused
struct sysctl_oid
*oidp
, void *arg1
,
851 __unused
int arg2
, struct sysctl_req
*req
)
854 int sysctl_shminfo_ret
= 0;
856 error
= SYSCTL_OUT(req
, arg1
, sizeof(int64_t));
857 if (error
|| req
->newptr
== USER_ADDR_NULL
)
860 SYSV_SHM_SUBSYS_LOCK();
861 /* Set the values only if shared memory is not initialised */
863 if ((error
= SYSCTL_IN(req
, arg1
, sizeof(int64_t)))
865 sysctl_shminfo_ret
= error
;
866 goto sysctl_shminfo_out
;
869 if (arg1
== &shminfo
.shmmax
) {
870 if (shminfo
.shmmax
& PAGE_MASK_64
) {
871 shminfo
.shmmax
= (int64_t)-1;
872 sysctl_shminfo_ret
= EINVAL
;
873 goto sysctl_shminfo_out
;
877 /* Initialize only when all values are set */
878 if ((shminfo
.shmmax
!= (int64_t)-1) &&
879 (shminfo
.shmmin
!= (int64_t)-1) &&
880 (shminfo
.shmmni
!= (int64_t)-1) &&
881 (shminfo
.shmseg
!= (int64_t)-1) &&
882 (shminfo
.shmall
!= (int64_t)-1)) {
886 sysctl_shminfo_ret
= 0;
888 SYSV_SHM_SUBSYS_UNLOCK();
889 return sysctl_shminfo_ret
;
893 IPCS_shm_sysctl(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
894 __unused
int arg2
, struct sysctl_req
*req
)
899 struct IPCS_command u32
;
900 struct user_IPCS_command u64
;
902 struct shmid_ds shmid_ds32
; /* post conversion, 32 bit version */
904 size_t ipcs_sz
= sizeof(struct user_IPCS_command
);
905 size_t shmid_ds_sz
= sizeof(struct user_shmid_ds
);
906 struct proc
*p
= current_proc();
908 int ipcs__shminfo_ret
= 0;
910 SYSV_SHM_SUBSYS_LOCK();
914 goto ipcs_shm_sysctl_out
;
917 if (!IS_64BIT_PROCESS(p
)) {
918 ipcs_sz
= sizeof(struct IPCS_command
);
919 shmid_ds_sz
= sizeof(struct shmid_ds
);
922 /* Copy in the command structure */
923 if ((error
= SYSCTL_IN(req
, &ipcs
, ipcs_sz
)) != 0) {
924 goto ipcs_shm_sysctl_out
;
927 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
928 ipcs
.u64
.ipcs_data
= CAST_USER_ADDR_T(ipcs
.u32
.ipcs_data
);
930 /* Let us version this interface... */
931 if (ipcs
.u64
.ipcs_magic
!= IPCS_MAGIC
) {
933 goto ipcs_shm_sysctl_out
;
936 switch(ipcs
.u64
.ipcs_op
) {
937 case IPCS_SHM_CONF
: /* Obtain global configuration data */
938 if (ipcs
.u64
.ipcs_datalen
!= sizeof(struct shminfo
)) {
939 if (ipcs
.u64
.ipcs_cursor
!= 0) { /* fwd. compat. */
946 error
= copyout(&shminfo
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
949 case IPCS_SHM_ITER
: /* Iterate over existing segments */
950 cursor
= ipcs
.u64
.ipcs_cursor
;
951 if (cursor
< 0 || cursor
>= shminfo
.shmmni
) {
955 if (ipcs
.u64
.ipcs_datalen
!= (int)shmid_ds_sz
) {
959 for( ; cursor
< shminfo
.shmmni
; cursor
++) {
960 if (shmsegs
[cursor
].shm_perm
.mode
& SHMSEG_ALLOCATED
)
964 if (cursor
== shminfo
.shmmni
) {
969 shmid_dsp
= &shmsegs
[cursor
]; /* default: 64 bit */
972 * If necessary, convert the 64 bit kernel segment
973 * descriptor to a 32 bit user one.
975 if (!IS_64BIT_PROCESS(p
)) {
976 shmid_ds_64to32(shmid_dsp
, &shmid_ds32
);
977 shmid_dsp
= &shmid_ds32
;
979 error
= copyout(shmid_dsp
, ipcs
.u64
.ipcs_data
, ipcs
.u64
.ipcs_datalen
);
982 ipcs
.u64
.ipcs_cursor
= cursor
+ 1;
984 if (!IS_64BIT_PROCESS(p
)) /* convert in place */
985 ipcs
.u32
.ipcs_data
= CAST_DOWN(void *,ipcs
.u64
.ipcs_data
);
986 error
= SYSCTL_OUT(req
, &ipcs
, ipcs_sz
);
995 SYSV_SHM_SUBSYS_UNLOCK();
999 SYSCTL_NODE(_kern
, KERN_SYSV
, sysv
, CTLFLAG_RW
, 0, "SYSV");
1001 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMMAX
, shmmax
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1002 &shminfo
.shmmax
, 0, &sysctl_shminfo
,"Q","shmmax");
1004 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMMIN
, shmmin
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1005 &shminfo
.shmmin
, 0, &sysctl_shminfo
,"Q","shmmin");
1007 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMMNI
, shmmni
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1008 &shminfo
.shmmni
, 0, &sysctl_shminfo
,"Q","shmmni");
1010 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMSEG
, shmseg
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1011 &shminfo
.shmseg
, 0, &sysctl_shminfo
,"Q","shmseg");
1013 SYSCTL_PROC(_kern_sysv
, KSYSV_SHMALL
, shmall
, CTLTYPE_QUAD
| CTLFLAG_RW
,
1014 &shminfo
.shmall
, 0, &sysctl_shminfo
,"Q","shmall");
1016 SYSCTL_NODE(_kern_sysv
, OID_AUTO
, ipcs
, CTLFLAG_RW
, 0, "SYSVIPCS");
1018 SYSCTL_PROC(_kern_sysv_ipcs
, OID_AUTO
, shm
, CTLFLAG_RW
|CTLFLAG_ANYBODY
,
1019 0, 0, IPCS_shm_sysctl
,
1020 "S,IPCS_shm_command",
1021 "ipcs shm command interface");