]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/sysv_shm.c
4b111d3ad955ef2bc8fd3684c32663e29294c2e2
[apple/xnu.git] / bsd / kern / sysv_shm.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
24
25 /*
26 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
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
39 * Hannum.
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.
42 *
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.
53 */
54
55
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>
64 #include <sys/mman.h>
65 #include <sys/stat.h>
66 #include <sys/sysctl.h>
67 #include <sys/ipcs.h>
68 #include <sys/sysent.h>
69 #include <sys/sysproto.h>
70
71 #include <bsm/audit_kernel.h>
72
73 #include <mach/mach_types.h>
74 #include <mach/vm_inherit.h>
75 #include <mach/vm_map.h>
76
77 #include <mach/mach_vm.h>
78
79 #include <vm/vm_map.h>
80 #include <vm/vm_shared_memory_server.h>
81 #include <vm/vm_protos.h>
82
83 #include <kern/locks.h>
84
85 static void shminit(void *);
86 #if 0
87 SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL)
88 #endif 0
89
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;
94
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)
97
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);
103
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,
108 (sy_call_t *)shmctl
109 };
110
111 #define SHMSEG_FREE 0x0200
112 #define SHMSEG_REMOVED 0x0400
113 #define SHMSEG_ALLOCATED 0x0800
114 #define SHMSEG_WANTED 0x1000
115
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;
119
120 struct shm_handle {
121 void * shm_object; /* vm_offset_t kva; */
122 };
123
124 struct shmmap_state {
125 mach_vm_address_t va; /* user address */
126 int shmid; /* segment id */
127 };
128
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);
133
134 #ifdef __APPLE_API_PRIVATE
135 struct shminfo shminfo = {
136 -1, /* SHMMAX 4096 *1024 */
137 -1, /* SHMMIN = 1 */
138 -1, /* SHMMNI = 1 */
139 -1, /* SHMSEG = 8 */
140 -1 /* SHMALL = 1024 */
141 };
142 #endif /* __APPLE_API_PRIVATE */
143
144 void sysv_shm_lock_init(void);
145
146 static __inline__ time_t
147 sysv_shmtime(void)
148 {
149 struct timeval tv;
150 microtime(&tv);
151 return (tv.tv_sec);
152 }
153
154 /*
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.
157 *
158 * NOTE: Source and target may *NOT* overlap! (target is smaller)
159 */
160 static void
161 shmid_ds_64to32(struct user_shmid_ds *in, struct shmid_ds *out)
162 {
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);
172 }
173
174 /*
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
177 * the beginning.
178 */
179 static void
180 shmid_ds_32to64(struct shmid_ds *in, struct user_shmid_ds *out)
181 {
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;
191 }
192
193
194 static int
195 shm_find_segment_by_key(key_t key)
196 {
197 int i;
198
199 for (i = 0; i < shminfo.shmmni; i++)
200 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
201 shmsegs[i].shm_perm.key == key)
202 return i;
203 return -1;
204 }
205
206 static struct user_shmid_ds *
207 shm_find_segment_by_shmid(int shmid)
208 {
209 int segnum;
210 struct user_shmid_ds *shmseg;
211
212 segnum = IPCID_TO_IX(shmid);
213 if (segnum < 0 || segnum >= shminfo.shmmni)
214 return NULL;
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))
219 return NULL;
220 return shmseg;
221 }
222
223 static void
224 shm_deallocate_segment(struct user_shmid_ds *shmseg)
225 {
226 struct shm_handle *shm_handle;
227 mach_vm_size_t size;
228
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);
236 shm_nused--;
237 shmseg->shm_perm.mode = SHMSEG_FREE;
238 }
239
240 static int
241 shm_delete_mapping(__unused struct proc *p, struct shmmap_state *shmmap_s,
242 int deallocate)
243 {
244 struct user_shmid_ds *shmseg;
245 int segnum, result;
246 mach_vm_size_t size;
247
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? */
251 if (deallocate) {
252 result = mach_vm_deallocate(current_map(), shmmap_s->va, size);
253 if (result != KERN_SUCCESS)
254 return EINVAL;
255 }
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;
262 }
263 return 0;
264 }
265
266 int
267 shmdt(struct proc *p, struct shmdt_args *uap, register_t *retval)
268 {
269 struct shmmap_state *shmmap_s;
270 int i;
271 int shmdtret = 0;
272
273 // LP64todo - fix this
274 AUDIT_ARG(svipc_addr, CAST_DOWN(void *,uap->shmaddr));
275
276 SYSV_SHM_SUBSYS_LOCK();
277
278 if (!shm_inited) {
279 shmdtret = EINVAL;
280 goto shmdt_out;
281 }
282 shmmap_s = (struct shmmap_state *)p->vm_shm;
283 if (shmmap_s == NULL) {
284 shmdtret = EINVAL;
285 goto shmdt_out;
286 }
287
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)
291 break;
292 if (i == shminfo.shmseg) {
293 shmdtret = EINVAL;
294 goto shmdt_out;
295 }
296 i = shm_delete_mapping(p, shmmap_s, 1);
297
298 if (i == 0)
299 *retval = 0;
300 shmdtret = i;
301 shmdt_out:
302 SYSV_SHM_SUBSYS_UNLOCK();
303 return shmdtret;
304 }
305
306 int
307 shmat(struct proc *p, struct shmat_args *uap, register_t *retval)
308 {
309 int error, i, flags;
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 */
315 vm_prot_t prot;
316 size_t size;
317 kern_return_t rv;
318 int shmat_ret = 0;
319
320 AUDIT_ARG(svipc_id, uap->shmid);
321 // LP64todo - fix this
322 AUDIT_ARG(svipc_addr, CAST_DOWN(void *,uap->shmaddr));
323
324 SYSV_SHM_SUBSYS_LOCK();
325
326 if (!shm_inited) {
327 shmat_ret = EINVAL;
328 goto shmat_out;
329 }
330
331 shmmap_s = (struct shmmap_state *)p->vm_shm;
332
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) {
337 shmat_ret = ENOMEM;
338 goto shmat_out;
339 }
340 for (i = 0; i < shminfo.shmseg; i++)
341 shmmap_s[i].shmid = -1;
342 p->vm_shm = (caddr_t)shmmap_s;
343 }
344 shmseg = shm_find_segment_by_shmid(uap->shmid);
345 if (shmseg == NULL) {
346 shmat_ret = EINVAL;
347 goto shmat_out;
348 }
349
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);
353 if (error) {
354 shmat_ret = error;
355 goto shmat_out;
356 }
357
358 for (i = 0; i < shminfo.shmseg; i++) {
359 if (shmmap_s->shmid == -1)
360 break;
361 shmmap_s++;
362 }
363 if (i >= shminfo.shmseg) {
364 shmat_ret = EMFILE;
365 goto shmat_out;
366 }
367
368 map_size = mach_vm_round_page(shmseg->shm_segsz);
369 prot = VM_PROT_READ;
370 if ((uap->shmflg & SHM_RDONLY) == 0)
371 prot |= VM_PROT_WRITE;
372 flags = MAP_ANON | MAP_SHARED;
373 if (uap->shmaddr)
374 flags |= MAP_FIXED;
375
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) {
380 shmat_ret = EINVAL;
381 goto shmat_out;
382 }
383
384 shm_handle = CAST_DOWN(void *, shmseg->shm_internal); /* tunnel */
385
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,
392 (mach_vm_offset_t)0,
393 FALSE,
394 prot,
395 prot,
396 VM_INHERIT_DEFAULT);
397 if (rv != KERN_SUCCESS)
398 goto out;
399
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);
403 goto out;
404 }
405
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 */
412 shmat_ret = 0;
413 goto shmat_out;
414 out:
415 switch (rv) {
416 case KERN_INVALID_ADDRESS:
417 case KERN_NO_SPACE:
418 shmat_ret = ENOMEM;
419 case KERN_PROTECTION_FAILURE:
420 shmat_ret = EACCES;
421 default:
422 shmat_ret = EINVAL;
423 }
424 shmat_out:
425 SYSV_SHM_SUBSYS_UNLOCK();
426 return shmat_ret;
427 }
428
429 static int
430 oshmctl(__unused void *p, __unused void *uap, __unused void *retval)
431 {
432 return EINVAL;
433 }
434
435 int
436 shmctl(__unused struct proc *p, struct shmctl_args *uap, register_t *retval)
437 {
438 int error;
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);
443
444 int shmctl_ret = 0;
445
446 AUDIT_ARG(svipc_cmd, uap->cmd);
447 AUDIT_ARG(svipc_id, uap->shmid);
448
449 SYSV_SHM_SUBSYS_LOCK();
450
451 if (!shm_inited) {
452 shmctl_ret = EINVAL;
453 goto shmctl_out;
454 }
455
456 if (!IS_64BIT_PROCESS(p))
457 shmid_ds_sz = sizeof(struct shmid_ds);
458
459 shmseg = shm_find_segment_by_shmid(uap->shmid);
460 if (shmseg == NULL) {
461 shmctl_ret = EINVAL;
462 goto shmctl_out;
463 }
464
465 /* XXAUDIT: This is the perms BEFORE any change by this call. This
466 * may not be what is desired.
467 */
468 AUDIT_ARG(svipc_perm, &shmseg->shm_perm);
469
470 switch (uap->cmd) {
471 case IPC_STAT:
472 error = ipcperm(cred, &shmseg->shm_perm, IPC_R);
473 if (error) {
474 shmctl_ret = error;
475 goto shmctl_out;
476 }
477
478 if (IS_64BIT_PROCESS(p)) {
479 error = copyout(shmseg, uap->buf, sizeof(struct user_shmid_ds));
480 } else {
481 struct shmid_ds shmid_ds32;
482 shmid_ds_64to32(shmseg, &shmid_ds32);
483 error = copyout(&shmid_ds32, uap->buf, sizeof(struct shmid_ds));
484 }
485 if (error) {
486 shmctl_ret = error;
487 goto shmctl_out;
488 }
489 break;
490 case IPC_SET:
491 error = ipcperm(cred, &shmseg->shm_perm, IPC_M);
492 if (error) {
493 shmctl_ret = error;
494 goto shmctl_out;
495 }
496 if (IS_64BIT_PROCESS(p)) {
497 error = copyin(uap->buf, &inbuf, sizeof(struct user_shmid_ds));
498 } else {
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);
502 }
503 if (error) {
504 shmctl_ret = error;
505 goto shmctl_out;
506 }
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();
513 break;
514 case IPC_RMID:
515 error = ipcperm(cred, &shmseg->shm_perm, IPC_M);
516 if (error) {
517 shmctl_ret = error;
518 goto shmctl_out;
519 }
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);
525 }
526 break;
527 #if 0
528 case SHM_LOCK:
529 case SHM_UNLOCK:
530 #endif
531 default:
532 shmctl_ret = EINVAL;
533 goto shmctl_out;
534 }
535 *retval = 0;
536 shmctl_ret = 0;
537 shmctl_out:
538 SYSV_SHM_SUBSYS_UNLOCK();
539 return shmctl_ret;
540 }
541
542 static int
543 shmget_existing(struct shmget_args *uap, int mode, int segnum, int *retval)
544 {
545 struct user_shmid_ds *shmseg;
546 int error;
547
548 shmseg = &shmsegs[segnum];
549 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
550 /*
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).
554 */
555 shmseg->shm_perm.mode |= SHMSEG_WANTED;
556 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
557 if (error)
558 return error;
559 return EAGAIN;
560 }
561 error = ipcperm(kauth_cred_get(), &shmseg->shm_perm, mode);
562 if (error)
563 return error;
564 if (uap->size && uap->size > shmseg->shm_segsz)
565 return EINVAL;
566 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
567 return EEXIST;
568 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
569 return 0;
570 }
571
572 static int
573 shmget_allocate_segment(struct proc *p, struct shmget_args *uap, int mode,
574 int *retval)
575 {
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;
580 kern_return_t kret;
581 vm_offset_t user_addr;
582 void * mem_object;
583
584 if (uap->size < (user_size_t)shminfo.shmmin ||
585 uap->size > (user_size_t)shminfo.shmmax)
586 return EINVAL;
587 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
588 return ENOSPC;
589 size = mach_vm_round_page(uap->size);
590 if (shm_committed + btoc(size) > shminfo.shmall)
591 return ENOMEM;
592 if (shm_last_free < 0) {
593 for (i = 0; i < shminfo.shmmni; i++)
594 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
595 break;
596 if (i == shminfo.shmmni)
597 panic("shmseg free count inconsistent");
598 segnum = i;
599 } else {
600 segnum = shm_last_free;
601 shm_last_free = -1;
602 }
603 shmseg = &shmsegs[segnum];
604 /*
605 * In case we sleep in malloc(), mark the segment present but deleted
606 * so that noone else tries to create the same key.
607 */
608 kret = vm_allocate(current_map(), &user_addr, size, VM_FLAGS_ANYWHERE);
609 if (kret != KERN_SUCCESS)
610 goto out;
611
612 kret = mach_make_memory_entry (current_map(), &size, user_addr,
613 VM_PROT_DEFAULT, (mem_entry_name_port_t *)&mem_object, 0);
614
615 if (kret != KERN_SUCCESS)
616 goto out;
617
618 vm_deallocate(current_map(), user_addr, size);
619
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);
627 mem_object = NULL;
628 goto out;
629 }
630 shm_handle->shm_object = mem_object;
631 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
632
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);
644 shm_nused++;
645 AUDIT_ARG(svipc_perm, &shmseg->shm_perm);
646 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
647 /*
648 * Somebody else wanted this key while we were asleep. Wake
649 * them up now.
650 */
651 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
652 wakeup((caddr_t)shmseg);
653 }
654 *retval = shmid;
655 AUDIT_ARG(svipc_id, shmid);
656 return 0;
657 out:
658 switch (kret) {
659 case KERN_INVALID_ADDRESS:
660 case KERN_NO_SPACE:
661 return (ENOMEM);
662 case KERN_PROTECTION_FAILURE:
663 return (EACCES);
664 default:
665 return (EINVAL);
666 }
667
668 }
669
670 int
671 shmget(struct proc *p, struct shmget_args *uap, register_t *retval)
672 {
673 int segnum, mode, error;
674 int shmget_ret = 0;
675
676 /* Auditing is actually done in shmget_allocate_segment() */
677
678 SYSV_SHM_SUBSYS_LOCK();
679
680 if (!shm_inited) {
681 shmget_ret = EINVAL;
682 goto shmget_out;
683 }
684
685 mode = uap->shmflg & ACCESSPERMS;
686 if (uap->key != IPC_PRIVATE) {
687 again:
688 segnum = shm_find_segment_by_key(uap->key);
689 if (segnum >= 0) {
690 error = shmget_existing(uap, mode, segnum, retval);
691 if (error == EAGAIN)
692 goto again;
693 shmget_ret = error;
694 goto shmget_out;
695 }
696 if ((uap->shmflg & IPC_CREAT) == 0) {
697 shmget_ret = ENOENT;
698 goto shmget_out;
699 }
700 }
701 shmget_ret = shmget_allocate_segment(p, uap, mode, retval);
702 shmget_out:
703 SYSV_SHM_SUBSYS_UNLOCK();
704 return shmget_ret;
705 /*NOTREACHED*/
706
707 }
708
709 /* XXX actually varargs. */
710 int
711 shmsys(struct proc *p, struct shmsys_args *uap, register_t *retval)
712 {
713
714 /* The routine that we are dispatching already does this */
715
716 if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
717 return EINVAL;
718 return ((*shmcalls[uap->which])(p, &uap->a2, retval));
719 }
720
721 /*
722 * Return 0 on success, 1 on failure.
723 */
724 int
725 shmfork(struct proc *p1, struct proc *p2)
726 {
727 struct shmmap_state *shmmap_s;
728 size_t size;
729 int i;
730 int shmfork_ret = 0;
731
732 SYSV_SHM_SUBSYS_LOCK();
733
734 if (!shm_inited) {
735 shmfork_ret = 0;
736 goto shmfork_out;
737 }
738
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++;
747 shmfork_ret = 0;
748 goto shmfork_out;
749 }
750
751 shmfork_ret = 1; /* failed to copy to child - ENOMEM */
752 shmfork_out:
753 SYSV_SHM_SUBSYS_UNLOCK();
754 return shmfork_ret;
755 }
756
757 void
758 shmexit(struct proc *p)
759 {
760 struct shmmap_state *shmmap_s;
761 int i;
762
763 shmmap_s = (struct shmmap_state *)p->vm_shm;
764
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);
770 p->vm_shm = NULL;
771 SYSV_SHM_SUBSYS_UNLOCK();
772 }
773
774 /*
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.
779 */
780 __private_extern__ void
781 shmexec(struct proc *p)
782 {
783 struct shmmap_state *shmmap_s;
784 int i;
785
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);
792 p->vm_shm = NULL;
793 SYSV_SHM_SUBSYS_UNLOCK();
794 }
795
796 void
797 shminit(__unused void *dummy)
798 {
799 int i;
800 int s;
801
802 if (!shm_inited) {
803 /*
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.
809 */
810 s = sizeof(struct user_shmid_ds) * shminfo.shmmni;
811
812 MALLOC(shmsegs, struct user_shmid_ds *, s, M_SHM, M_WAITOK);
813 if (shmsegs == NULL) {
814 /* XXX fail safely: leave shared memory uninited */
815 return;
816 }
817 for (i = 0; i < shminfo.shmmni; i++) {
818 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
819 shmsegs[i].shm_perm.seq = 0;
820 }
821 shm_last_free = 0;
822 shm_nused = 0;
823 shm_committed = 0;
824 shm_inited = 1;
825 }
826 }
827 /* Initialize the mutex governing access to the SysV shm subsystem */
828 __private_extern__ void
829 sysv_shm_lock_init( void )
830 {
831
832 sysv_shm_subsys_lck_grp_attr = lck_grp_attr_alloc_init();
833 lck_grp_attr_setstat(sysv_shm_subsys_lck_grp_attr);
834
835 sysv_shm_subsys_lck_grp = lck_grp_alloc_init("sysv_shm_subsys_lock", sysv_shm_subsys_lck_grp_attr);
836
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);
840 }
841
842 /* (struct sysctl_oid *oidp, void *arg1, int arg2, \
843 struct sysctl_req *req) */
844 static int
845 sysctl_shminfo(__unused struct sysctl_oid *oidp, void *arg1,
846 __unused int arg2, struct sysctl_req *req)
847 {
848 int error = 0;
849 int sysctl_shminfo_ret = 0;
850
851 error = SYSCTL_OUT(req, arg1, sizeof(user_ssize_t));
852 if (error || req->newptr == USER_ADDR_NULL)
853 return(error);
854
855 SYSV_SHM_SUBSYS_LOCK();
856 /* Set the values only if shared memory is not initialised */
857 if (!shm_inited) {
858 if ((error = SYSCTL_IN(req, arg1, sizeof(user_ssize_t)))
859 != 0) {
860 sysctl_shminfo_ret = error;
861 goto sysctl_shminfo_out;
862 }
863
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;
869 }
870 }
871
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)) {
878 shminit(NULL);
879 }
880 }
881 sysctl_shminfo_ret = 0;
882 sysctl_shminfo_out:
883 SYSV_SHM_SUBSYS_UNLOCK();
884 return sysctl_shminfo_ret;
885 }
886
887 static int
888 IPCS_shm_sysctl(__unused struct sysctl_oid *oidp, __unused void *arg1,
889 __unused int arg2, struct sysctl_req *req)
890 {
891 int error;
892 int cursor;
893 union {
894 struct IPCS_command u32;
895 struct user_IPCS_command u64;
896 } ipcs;
897 struct shmid_ds shmid_ds32; /* post conversion, 32 bit version */
898 void *shmid_dsp;
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();
902
903 int ipcs__shminfo_ret = 0;
904
905 SYSV_SHM_SUBSYS_LOCK();
906
907 if (!shm_inited) {
908 error = EINVAL;
909 goto ipcs_shm_sysctl_out;
910 }
911
912 if (!IS_64BIT_PROCESS(p)) {
913 ipcs_sz = sizeof(struct IPCS_command);
914 shmid_ds_sz = sizeof(struct shmid_ds);
915 }
916
917 /* Copy in the command structure */
918 if ((error = SYSCTL_IN(req, &ipcs, ipcs_sz)) != 0) {
919 goto ipcs_shm_sysctl_out;
920 }
921
922 if (!IS_64BIT_PROCESS(p)) /* convert in place */
923 ipcs.u64.ipcs_data = CAST_USER_ADDR_T(ipcs.u32.ipcs_data);
924
925 /* Let us version this interface... */
926 if (ipcs.u64.ipcs_magic != IPCS_MAGIC) {
927 error = EINVAL;
928 goto ipcs_shm_sysctl_out;
929 }
930
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. */
935 error = ENOMEM;
936 break;
937 }
938 error = ERANGE;
939 break;
940 }
941 error = copyout(&shminfo, ipcs.u64.ipcs_data, ipcs.u64.ipcs_datalen);
942 break;
943
944 case IPCS_SHM_ITER: /* Iterate over existing segments */
945 cursor = ipcs.u64.ipcs_cursor;
946 if (cursor < 0 || cursor >= shminfo.shmmni) {
947 error = ERANGE;
948 break;
949 }
950 if (ipcs.u64.ipcs_datalen != (int)shmid_ds_sz) {
951 error = ENOMEM;
952 break;
953 }
954 for( ; cursor < shminfo.shmmni; cursor++) {
955 if (shmsegs[cursor].shm_perm.mode & SHMSEG_ALLOCATED)
956 break;
957 continue;
958 }
959 if (cursor == shminfo.shmmni) {
960 error = ENOENT;
961 break;
962 }
963
964 shmid_dsp = &shmsegs[cursor]; /* default: 64 bit */
965
966 /*
967 * If necessary, convert the 64 bit kernel segment
968 * descriptor to a 32 bit user one.
969 */
970 if (!IS_64BIT_PROCESS(p)) {
971 shmid_ds_64to32(shmid_dsp, &shmid_ds32);
972 shmid_dsp = &shmid_ds32;
973 }
974 error = copyout(shmid_dsp, ipcs.u64.ipcs_data, ipcs.u64.ipcs_datalen);
975 if (!error) {
976 /* update cursor */
977 ipcs.u64.ipcs_cursor = cursor + 1;
978
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);
982 }
983 break;
984
985 default:
986 error = EINVAL;
987 break;
988 }
989 ipcs_shm_sysctl_out:
990 SYSV_SHM_SUBSYS_UNLOCK();
991 return(error);
992 }
993
994 SYSCTL_NODE(_kern, KERN_SYSV, sysv, CTLFLAG_RW, 0, "SYSV");
995
996 SYSCTL_PROC(_kern_sysv, KSYSV_SHMMAX, shmmax, CTLTYPE_QUAD | CTLFLAG_RW,
997 &shminfo.shmmax, 0, &sysctl_shminfo ,"Q","shmmax");
998
999 SYSCTL_PROC(_kern_sysv, KSYSV_SHMMIN, shmmin, CTLTYPE_QUAD | CTLFLAG_RW,
1000 &shminfo.shmmin, 0, &sysctl_shminfo ,"Q","shmmin");
1001
1002 SYSCTL_PROC(_kern_sysv, KSYSV_SHMMNI, shmmni, CTLTYPE_QUAD | CTLFLAG_RW,
1003 &shminfo.shmmni, 0, &sysctl_shminfo ,"Q","shmmni");
1004
1005 SYSCTL_PROC(_kern_sysv, KSYSV_SHMSEG, shmseg, CTLTYPE_QUAD | CTLFLAG_RW,
1006 &shminfo.shmseg, 0, &sysctl_shminfo ,"Q","shmseg");
1007
1008 SYSCTL_PROC(_kern_sysv, KSYSV_SHMALL, shmall, CTLTYPE_QUAD | CTLFLAG_RW,
1009 &shminfo.shmall, 0, &sysctl_shminfo ,"Q","shmall");
1010
1011 SYSCTL_NODE(_kern_sysv, OID_AUTO, ipcs, CTLFLAG_RW, 0, "SYSVIPCS");
1012
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");