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