Merge branch 'master' of git://git.dragonflybsd.org/dragonfly
[dragonfly.git] / sys / kern / sysv_shm.c
1 /* $FreeBSD: src/sys/kern/sysv_shm.c,v 1.45.2.6 2002/10/22 20:45:03 fjoe Exp $ */
2 /* $DragonFly: src/sys/kern/sysv_shm.c,v 1.21 2008/01/06 16:55:51 swildner Exp $ */
3 /*      $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $      */
4
5 /*
6  * Copyright (c) 1994 Adam Glass and Charles Hannum.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Adam Glass and Charles
19  *      Hannum.
20  * 4. The names of the authors may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include "opt_compat.h"
36 #include "opt_sysvipc.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysproto.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
43 #include <sys/shm.h>
44 #include <sys/proc.h>
45 #include <sys/malloc.h>
46 #include <sys/mman.h>
47 #include <sys/stat.h>
48 #include <sys/sysent.h>
49 #include <sys/jail.h>
50
51 #include <sys/mplock2.h>
52
53 #include <vm/vm.h>
54 #include <vm/vm_param.h>
55 #include <sys/lock.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_pager.h>
61
62 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
63
64 struct oshmctl_args;
65 static int sys_oshmctl (struct proc *p, struct oshmctl_args *uap);
66
67 static int shmget_allocate_segment (struct proc *p, struct shmget_args *uap, int mode);
68 static int shmget_existing (struct proc *p, struct shmget_args *uap, int mode, int segnum);
69
70 /* XXX casting to (sy_call_t *) is bogus, as usual. */
71 static sy_call_t *shmcalls[] = {
72         (sy_call_t *)sys_shmat, (sy_call_t *)sys_oshmctl,
73         (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
74         (sy_call_t *)sys_shmctl
75 };
76
77 #define SHMSEG_FREE             0x0200
78 #define SHMSEG_REMOVED          0x0400
79 #define SHMSEG_ALLOCATED        0x0800
80 #define SHMSEG_WANTED           0x1000
81
82 static int shm_last_free, shm_nused, shm_committed, shmalloced;
83 static struct shmid_ds  *shmsegs;
84
85 struct shm_handle {
86         /* vm_offset_t kva; */
87         vm_object_t shm_object;
88 };
89
90 struct shmmap_state {
91         vm_offset_t va;
92         int shmid;
93 };
94
95 static void shm_deallocate_segment (struct shmid_ds *);
96 static int shm_find_segment_by_key (key_t);
97 static struct shmid_ds *shm_find_segment_by_shmid (int);
98 static int shm_delete_mapping (struct vmspace *vm, struct shmmap_state *);
99 static void shmrealloc (void);
100 static void shminit (void *);
101
102 /*
103  * Tuneable values
104  */
105 #ifndef SHMMAXPGS
106 #define SHMMAXPGS       8192    /* note: sysv shared memory is swap backed */
107 #endif
108 #ifndef SHMMAX
109 #define SHMMAX  (SHMMAXPGS*PAGE_SIZE)
110 #endif
111 #ifndef SHMMIN
112 #define SHMMIN  1
113 #endif
114 #ifndef SHMMNI
115 #define SHMMNI  192
116 #endif
117 #ifndef SHMSEG
118 #define SHMSEG  128
119 #endif
120 #ifndef SHMALL
121 #define SHMALL  (SHMMAXPGS)
122 #endif
123
124 struct  shminfo shminfo = {
125         SHMMAX,
126         SHMMIN,
127         SHMMNI,
128         SHMSEG,
129         SHMALL
130 };
131
132 static int shm_use_phys;
133
134 TUNABLE_INT("kern.ipc.shmmin", &shminfo.shmmin);
135 TUNABLE_INT("kern.ipc.shmmni", &shminfo.shmmni);
136 TUNABLE_INT("kern.ipc.shmseg", &shminfo.shmseg);
137 TUNABLE_INT("kern.ipc.shmmaxpgs", &shminfo.shmall);
138 TUNABLE_INT("kern.ipc.shm_use_phys", &shm_use_phys);
139
140 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0, "");
141 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0, "");
142 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RD, &shminfo.shmmni, 0, "");
143 SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RW, &shminfo.shmseg, 0, "");
144 SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0, "");
145 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW, &shm_use_phys, 0, "");
146
147 static int
148 shm_find_segment_by_key(key_t key)
149 {
150         int i;
151
152         for (i = 0; i < shmalloced; i++)
153                 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
154                     shmsegs[i].shm_perm.key == key)
155                         return i;
156         return -1;
157 }
158
159 static struct shmid_ds *
160 shm_find_segment_by_shmid(int shmid)
161 {
162         int segnum;
163         struct shmid_ds *shmseg;
164
165         segnum = IPCID_TO_IX(shmid);
166         if (segnum < 0 || segnum >= shmalloced)
167                 return NULL;
168         shmseg = &shmsegs[segnum];
169         if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
170             != SHMSEG_ALLOCATED ||
171             shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
172                 return NULL;
173         return shmseg;
174 }
175
176 static void
177 shm_deallocate_segment(struct shmid_ds *shmseg)
178 {
179         struct shm_handle *shm_handle;
180         size_t size;
181
182         shm_handle = shmseg->shm_internal;
183         vm_object_deallocate(shm_handle->shm_object);
184         kfree((caddr_t)shm_handle, M_SHM);
185         shmseg->shm_internal = NULL;
186         size = round_page(shmseg->shm_segsz);
187         shm_committed -= btoc(size);
188         shm_nused--;
189         shmseg->shm_perm.mode = SHMSEG_FREE;
190 }
191
192 static int
193 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
194 {
195         struct shmid_ds *shmseg;
196         int segnum, result;
197         size_t size;
198
199         segnum = IPCID_TO_IX(shmmap_s->shmid);
200         shmseg = &shmsegs[segnum];
201         size = round_page(shmseg->shm_segsz);
202         result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
203         if (result != KERN_SUCCESS)
204                 return EINVAL;
205         shmmap_s->shmid = -1;
206         shmseg->shm_dtime = time_second;
207         if ((--shmseg->shm_nattch <= 0) &&
208             (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
209                 shm_deallocate_segment(shmseg);
210                 shm_last_free = segnum;
211         }
212         return 0;
213 }
214
215 /*
216  * MPALMOSTSAFE
217  */
218 int
219 sys_shmdt(struct shmdt_args *uap)
220 {
221         struct thread *td = curthread;
222         struct proc *p = td->td_proc;
223         struct shmmap_state *shmmap_s;
224         int i;
225         int error;
226
227         if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
228                 return (ENOSYS);
229
230         get_mplock();
231         shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
232         if (shmmap_s == NULL) {
233                 error = EINVAL;
234                 goto done;
235         }
236         for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
237                 if (shmmap_s->shmid != -1 &&
238                     shmmap_s->va == (vm_offset_t)uap->shmaddr)
239                         break;
240         }
241         if (i == shminfo.shmseg)
242                 error = EINVAL;
243         else
244                 error = shm_delete_mapping(p->p_vmspace, shmmap_s);
245 done:
246         rel_mplock();
247         return (error);
248 }
249
250 /*
251  * MPALMOSTSAFE
252  */
253 int
254 sys_shmat(struct shmat_args *uap)
255 {
256         struct thread *td = curthread;
257         struct proc *p = td->td_proc;
258         int error, i, flags;
259         struct shmid_ds *shmseg;
260         struct shmmap_state *shmmap_s = NULL;
261         struct shm_handle *shm_handle;
262         vm_offset_t attach_va;
263         vm_prot_t prot;
264         vm_size_t size;
265         int rv;
266
267         if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
268                 return (ENOSYS);
269
270         get_mplock();
271 again:
272         shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
273         if (shmmap_s == NULL) {
274                 size = shminfo.shmseg * sizeof(struct shmmap_state);
275                 shmmap_s = kmalloc(size, M_SHM, M_WAITOK);
276                 for (i = 0; i < shminfo.shmseg; i++)
277                         shmmap_s[i].shmid = -1;
278                 if (p->p_vmspace->vm_shm != NULL) {
279                         kfree(shmmap_s, M_SHM);
280                         goto again;
281                 }
282                 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
283         }
284         shmseg = shm_find_segment_by_shmid(uap->shmid);
285         if (shmseg == NULL) {
286                 error = EINVAL;
287                 goto done;
288         }
289         error = ipcperm(p, &shmseg->shm_perm,
290                         (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
291         if (error)
292                 goto done;
293         for (i = 0; i < shminfo.shmseg; i++) {
294                 if (shmmap_s->shmid == -1)
295                         break;
296                 shmmap_s++;
297         }
298         if (i >= shminfo.shmseg) {
299                 error = EMFILE;
300                 goto done;
301         }
302         size = round_page(shmseg->shm_segsz);
303 #ifdef VM_PROT_READ_IS_EXEC
304         prot = VM_PROT_READ | VM_PROT_EXECUTE;
305 #else
306         prot = VM_PROT_READ;
307 #endif
308         if ((uap->shmflg & SHM_RDONLY) == 0)
309                 prot |= VM_PROT_WRITE;
310         flags = MAP_ANON | MAP_SHARED;
311         if (uap->shmaddr) {
312                 flags |= MAP_FIXED;
313                 if (uap->shmflg & SHM_RND) {
314                         attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
315                 } else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0) {
316                         attach_va = (vm_offset_t)uap->shmaddr;
317                 } else {
318                         error = EINVAL;
319                         goto done;
320                 }
321         } else {
322                 /*
323                  * This is just a hint to vm_map_find() about where to put it.
324                  */
325                 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr + maxtsiz + maxdsiz);
326         }
327
328         shm_handle = shmseg->shm_internal;
329         vm_object_reference(shm_handle->shm_object);
330         rv = vm_map_find(&p->p_vmspace->vm_map, 
331                          shm_handle->shm_object, 0,
332                          &attach_va,
333                          size, PAGE_SIZE,
334                          ((flags & MAP_FIXED) ? 0 : 1), 
335                          VM_MAPTYPE_NORMAL,
336                          prot, prot,
337                          0);
338         if (rv != KERN_SUCCESS) {
339                 vm_object_deallocate(shm_handle->shm_object);
340                 error = ENOMEM;
341                 goto done;
342         }
343         vm_map_inherit(&p->p_vmspace->vm_map,
344                        attach_va, attach_va + size, VM_INHERIT_SHARE);
345
346         KKASSERT(shmmap_s->shmid == -1);
347         shmmap_s->va = attach_va;
348         shmmap_s->shmid = uap->shmid;
349         shmseg->shm_lpid = p->p_pid;
350         shmseg->shm_atime = time_second;
351         shmseg->shm_nattch++;
352         uap->sysmsg_result = attach_va;
353         error = 0;
354 done:
355         rel_mplock();
356         return error;
357 }
358
359 struct oshmid_ds {
360         struct  ipc_perm shm_perm;      /* operation perms */
361         int     shm_segsz;              /* size of segment (bytes) */
362         ushort  shm_cpid;               /* pid, creator */
363         ushort  shm_lpid;               /* pid, last operation */
364         short   shm_nattch;             /* no. of current attaches */
365         time_t  shm_atime;              /* last attach time */
366         time_t  shm_dtime;              /* last detach time */
367         time_t  shm_ctime;              /* last change time */
368         void    *shm_handle;            /* internal handle for shm segment */
369 };
370
371 struct oshmctl_args {
372         struct sysmsg sysmsg;
373         int shmid;
374         int cmd;
375         struct oshmid_ds *ubuf;
376 };
377
378 /*
379  * MPALMOSTSAFE
380  */
381 static int
382 sys_oshmctl(struct proc *p, struct oshmctl_args *uap)
383 {
384 #ifdef COMPAT_43
385         struct thread *td = curthread;
386         struct shmid_ds *shmseg;
387         struct oshmid_ds outbuf;
388         int error;
389
390         if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
391                 return (ENOSYS);
392
393         get_mplock();
394         shmseg = shm_find_segment_by_shmid(uap->shmid);
395         if (shmseg == NULL) {
396                 error = EINVAL;
397                 goto done;
398         }
399
400         switch (uap->cmd) {
401         case IPC_STAT:
402                 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
403                 if (error)
404                         break;
405                 outbuf.shm_perm = shmseg->shm_perm;
406                 outbuf.shm_segsz = shmseg->shm_segsz;
407                 outbuf.shm_cpid = shmseg->shm_cpid;
408                 outbuf.shm_lpid = shmseg->shm_lpid;
409                 outbuf.shm_nattch = shmseg->shm_nattch;
410                 outbuf.shm_atime = shmseg->shm_atime;
411                 outbuf.shm_dtime = shmseg->shm_dtime;
412                 outbuf.shm_ctime = shmseg->shm_ctime;
413                 outbuf.shm_handle = shmseg->shm_internal;
414                 error = copyout((caddr_t)&outbuf, uap->ubuf, sizeof(outbuf));
415                 break;
416         default:
417                 /* XXX casting to (sy_call_t *) is bogus, as usual. */
418                 error = sys_shmctl((struct shmctl_args *)uap);
419         }
420 done:
421         rel_mplock();
422         return error;
423 #else
424         return EINVAL;
425 #endif
426 }
427
428 /*
429  * MPALMOSTSAFE
430  */
431 int
432 sys_shmctl(struct shmctl_args *uap)
433 {
434         struct thread *td = curthread;
435         struct proc *p = td->td_proc;
436         int error;
437         struct shmid_ds inbuf;
438         struct shmid_ds *shmseg;
439
440         if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
441                 return (ENOSYS);
442
443         get_mplock();
444         shmseg = shm_find_segment_by_shmid(uap->shmid);
445         if (shmseg == NULL) {
446                 error = EINVAL;
447                 goto done;
448         }
449
450         switch (uap->cmd) {
451         case IPC_STAT:
452                 error = ipcperm(p, &shmseg->shm_perm, IPC_R);
453                 if (error == 0)
454                         error = copyout(shmseg, uap->buf, sizeof(inbuf));
455                 break;
456         case IPC_SET:
457                 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
458                 if (error == 0)
459                         error = copyin(uap->buf, &inbuf, sizeof(inbuf));
460                 if (error == 0) {
461                         shmseg->shm_perm.uid = inbuf.shm_perm.uid;
462                         shmseg->shm_perm.gid = inbuf.shm_perm.gid;
463                         shmseg->shm_perm.mode =
464                             (shmseg->shm_perm.mode & ~ACCESSPERMS) |
465                             (inbuf.shm_perm.mode & ACCESSPERMS);
466                         shmseg->shm_ctime = time_second;
467                 }
468                 break;
469         case IPC_RMID:
470                 error = ipcperm(p, &shmseg->shm_perm, IPC_M);
471                 if (error == 0) {
472                         shmseg->shm_perm.key = IPC_PRIVATE;
473                         shmseg->shm_perm.mode |= SHMSEG_REMOVED;
474                         if (shmseg->shm_nattch <= 0) {
475                                 shm_deallocate_segment(shmseg);
476                                 shm_last_free = IPCID_TO_IX(uap->shmid);
477                         }
478                 }
479                 break;
480 #if 0
481         case SHM_LOCK:
482         case SHM_UNLOCK:
483 #endif
484         default:
485                 error = EINVAL;
486                 break;
487         }
488 done:
489         rel_mplock();
490         return error;
491 }
492
493 static int
494 shmget_existing(struct proc *p, struct shmget_args *uap, int mode, int segnum)
495 {
496         struct shmid_ds *shmseg;
497         int error;
498
499         shmseg = &shmsegs[segnum];
500         if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
501                 /*
502                  * This segment is in the process of being allocated.  Wait
503                  * until it's done, and look the key up again (in case the
504                  * allocation failed or it was freed).
505                  */
506                 shmseg->shm_perm.mode |= SHMSEG_WANTED;
507                 error = tsleep((caddr_t)shmseg, PCATCH, "shmget", 0);
508                 if (error)
509                         return error;
510                 return EAGAIN;
511         }
512         if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
513                 return EEXIST;
514         error = ipcperm(p, &shmseg->shm_perm, mode);
515         if (error)
516                 return error;
517         if (uap->size && uap->size > shmseg->shm_segsz)
518                 return EINVAL;
519         uap->sysmsg_result = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
520         return 0;
521 }
522
523 static int
524 shmget_allocate_segment(struct proc *p, struct shmget_args *uap, int mode)
525 {
526         int i, segnum, shmid, size;
527         struct ucred *cred = p->p_ucred;
528         struct shmid_ds *shmseg;
529         struct shm_handle *shm_handle;
530
531         if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
532                 return EINVAL;
533         if (shm_nused >= shminfo.shmmni) /* any shmids left? */
534                 return ENOSPC;
535         size = round_page(uap->size);
536         if (shm_committed + btoc(size) > shminfo.shmall)
537                 return ENOMEM;
538         if (shm_last_free < 0) {
539                 shmrealloc();   /* maybe expand the shmsegs[] array */
540                 for (i = 0; i < shmalloced; i++)
541                         if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
542                                 break;
543                 if (i == shmalloced)
544                         return ENOSPC;
545                 segnum = i;
546         } else  {
547                 segnum = shm_last_free;
548                 shm_last_free = -1;
549         }
550         shmseg = &shmsegs[segnum];
551         /*
552          * In case we sleep in malloc(), mark the segment present but deleted
553          * so that noone else tries to create the same key.
554          */
555         shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
556         shmseg->shm_perm.key = uap->key;
557         shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
558         shm_handle = kmalloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
559         shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
560         
561         /*
562          * We make sure that we have allocated a pager before we need
563          * to.
564          */
565         if (shm_use_phys) {
566                 shm_handle->shm_object =
567                    phys_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0);
568         } else {
569                 shm_handle->shm_object =
570                    swap_pager_alloc(NULL, size, VM_PROT_DEFAULT, 0);
571         }
572         vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
573         vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
574
575         shmseg->shm_internal = shm_handle;
576         shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
577         shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
578         shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
579             (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
580         shmseg->shm_segsz = uap->size;
581         shmseg->shm_cpid = p->p_pid;
582         shmseg->shm_lpid = shmseg->shm_nattch = 0;
583         shmseg->shm_atime = shmseg->shm_dtime = 0;
584         shmseg->shm_ctime = time_second;
585         shm_committed += btoc(size);
586         shm_nused++;
587         if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
588                 /*
589                  * Somebody else wanted this key while we were asleep.  Wake
590                  * them up now.
591                  */
592                 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
593                 wakeup((caddr_t)shmseg);
594         }
595         uap->sysmsg_result = shmid;
596         return 0;
597 }
598
599 /*
600  * MPALMOSTSAFE
601  */
602 int
603 sys_shmget(struct shmget_args *uap)
604 {
605         struct thread *td = curthread;
606         struct proc *p = td->td_proc;
607         int segnum, mode, error;
608
609         if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
610                 return (ENOSYS);
611
612         mode = uap->shmflg & ACCESSPERMS;
613         get_mplock();
614
615         if (uap->key != IPC_PRIVATE) {
616         again:
617                 segnum = shm_find_segment_by_key(uap->key);
618                 if (segnum >= 0) {
619                         error = shmget_existing(p, uap, mode, segnum);
620                         if (error == EAGAIN)
621                                 goto again;
622                         goto done;
623                 }
624                 if ((uap->shmflg & IPC_CREAT) == 0) {
625                         error = ENOENT;
626                         goto done;
627                 }
628         }
629         error = shmget_allocate_segment(p, uap, mode);
630 done:
631         rel_mplock();
632         return (error);
633 }
634
635 /*
636  * shmsys_args(int which, int a2, ...) (VARARGS)
637  *
638  * MPALMOSTSAFE
639  */
640 int
641 sys_shmsys(struct shmsys_args *uap)
642 {
643         struct thread *td = curthread;
644         unsigned int which = (unsigned int)uap->which;
645         int error;
646
647         if (!jail_sysvipc_allowed && td->td_ucred->cr_prison != NULL)
648                 return (ENOSYS);
649
650         if (which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
651                 return EINVAL;
652         get_mplock();
653         bcopy(&uap->a2, &uap->which,
654                 sizeof(struct shmsys_args) - offsetof(struct shmsys_args, a2));
655         error = ((*shmcalls[which])(uap));
656         rel_mplock();
657
658         return(error);
659 }
660
661 void
662 shmfork(struct proc *p1, struct proc *p2)
663 {
664         struct shmmap_state *shmmap_s;
665         size_t size;
666         int i;
667
668         size = shminfo.shmseg * sizeof(struct shmmap_state);
669         shmmap_s = kmalloc(size, M_SHM, M_WAITOK);
670         bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
671         p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
672         for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
673                 if (shmmap_s->shmid != -1)
674                         shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
675 }
676
677 void
678 shmexit(struct vmspace *vm)
679 {
680         struct shmmap_state *base, *shm;
681         int i;
682
683         if ((base = (struct shmmap_state *)vm->vm_shm) != NULL) {
684                 vm->vm_shm = NULL;
685                 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
686                         if (shm->shmid != -1)
687                                 shm_delete_mapping(vm, shm);
688                 }
689                 kfree(base, M_SHM);
690         }
691 }
692
693 static void
694 shmrealloc(void)
695 {
696         int i;
697         struct shmid_ds *newsegs;
698
699         if (shmalloced >= shminfo.shmmni)
700                 return;
701
702         newsegs = kmalloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
703         for (i = 0; i < shmalloced; i++)
704                 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
705         for (; i < shminfo.shmmni; i++) {
706                 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
707                 shmsegs[i].shm_perm.seq = 0;
708         }
709         kfree(shmsegs, M_SHM);
710         shmsegs = newsegs;
711         shmalloced = shminfo.shmmni;
712 }
713
714 static void
715 shminit(void *dummy)
716 {
717         int i;
718
719         shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
720         shmalloced = shminfo.shmmni;
721         shmsegs = kmalloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
722         for (i = 0; i < shmalloced; i++) {
723                 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
724                 shmsegs[i].shm_perm.seq = 0;
725         }
726         shm_last_free = 0;
727         shm_nused = 0;
728         shm_committed = 0;
729 }
730 SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL);