sysv_shm.c revision 1.60.2.5 1 /* $NetBSD: sysv_shm.c,v 1.60.2.5 2002/04/17 00:06:19 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1994 Adam Glass and Charles M. Hannum. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by Adam Glass and Charles M.
54 * Hannum.
55 * 4. The names of the authors may not be used to endorse or promote products
56 * derived from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
59 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
62 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.60.2.5 2002/04/17 00:06:19 nathanw Exp $");
72
73 #define SYSVSHM
74
75 #include <sys/param.h>
76 #include <sys/kernel.h>
77 #include <sys/shm.h>
78 #include <sys/malloc.h>
79 #include <sys/mman.h>
80 #include <sys/stat.h>
81 #include <sys/sysctl.h>
82 #include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
83 #include <sys/syscallargs.h>
84
85 #include <uvm/uvm_extern.h>
86
87 struct shmid_ds *shm_find_segment_by_shmid __P((int, int));
88
89 /*
90 * Provides the following externally accessible functions:
91 *
92 * shminit(void); initialization
93 * shmexit(struct vmspace *) cleanup
94 * shmfork(struct vmspace *, struct vmspace *) fork handling
95 *
96 * Structures:
97 * shmsegs (an array of 'struct shmid_ds')
98 * per proc array of 'struct shmmap_state'
99 */
100
101 #define SHMSEG_FREE 0x0200
102 #define SHMSEG_REMOVED 0x0400
103 #define SHMSEG_ALLOCATED 0x0800
104 #define SHMSEG_WANTED 0x1000
105
106 int shm_last_free, shm_nused, shm_committed;
107 struct shmid_ds *shmsegs;
108
109 struct shm_handle {
110 struct uvm_object *shm_object;
111 };
112
113 struct shmmap_state {
114 vaddr_t va;
115 int shmid;
116 };
117
118 static int shm_find_segment_by_key __P((key_t));
119 static void shm_deallocate_segment __P((struct shmid_ds *));
120 static void shm_delete_mapping __P((struct vmspace *, struct shmmap_state *));
121 static int shmget_existing __P((struct proc *, struct sys_shmget_args *,
122 int, int, register_t *));
123 static int shmget_allocate_segment __P((struct proc *, struct sys_shmget_args *,
124 int, register_t *));
125
126 static int
127 shm_find_segment_by_key(key)
128 key_t key;
129 {
130 int i;
131
132 for (i = 0; i < shminfo.shmmni; i++)
133 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
134 shmsegs[i].shm_perm._key == key)
135 return i;
136 return -1;
137 }
138
139 struct shmid_ds *
140 shm_find_segment_by_shmid(shmid, findremoved)
141 int shmid;
142 int findremoved;
143 {
144 int segnum;
145 struct shmid_ds *shmseg;
146
147 segnum = IPCID_TO_IX(shmid);
148 if (segnum < 0 || segnum >= shminfo.shmmni)
149 return NULL;
150 shmseg = &shmsegs[segnum];
151 if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0)
152 return NULL;
153 if (!findremoved && ((shmseg->shm_perm.mode & SHMSEG_REMOVED) != 0))
154 return NULL;
155 if (shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid))
156 return NULL;
157 return shmseg;
158 }
159
160 static void
161 shm_deallocate_segment(shmseg)
162 struct shmid_ds *shmseg;
163 {
164 struct shm_handle *shm_handle;
165 size_t size;
166
167 shm_handle = shmseg->_shm_internal;
168 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
169 uao_detach(shm_handle->shm_object);
170 free((caddr_t)shm_handle, M_SHM);
171 shmseg->_shm_internal = NULL;
172 shm_committed -= btoc(size);
173 shmseg->shm_perm.mode = SHMSEG_FREE;
174 shm_nused--;
175 }
176
177 static void
178 shm_delete_mapping(vm, shmmap_s)
179 struct vmspace *vm;
180 struct shmmap_state *shmmap_s;
181 {
182 struct shmid_ds *shmseg;
183 int segnum;
184 size_t size;
185
186 segnum = IPCID_TO_IX(shmmap_s->shmid);
187 shmseg = &shmsegs[segnum];
188 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
189 uvm_deallocate(&vm->vm_map, shmmap_s->va, size);
190 shmmap_s->shmid = -1;
191 shmseg->shm_dtime = time.tv_sec;
192 if ((--shmseg->shm_nattch <= 0) &&
193 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
194 shm_deallocate_segment(shmseg);
195 shm_last_free = segnum;
196 }
197 }
198
199 int
200 sys_shmdt(l, v, retval)
201 struct lwp *l;
202 void *v;
203 register_t *retval;
204 {
205 struct sys_shmdt_args /* {
206 syscallarg(const void *) shmaddr;
207 } */ *uap = v;
208 struct proc *p = l->l_proc;
209 struct shmmap_state *shmmap_s;
210 int i;
211
212 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
213 if (shmmap_s == NULL)
214 return EINVAL;
215
216 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
217 if (shmmap_s->shmid != -1 &&
218 shmmap_s->va == (vaddr_t)SCARG(uap, shmaddr))
219 break;
220 if (i == shminfo.shmseg)
221 return EINVAL;
222 shm_delete_mapping(p->p_vmspace, shmmap_s);
223 return 0;
224 }
225
226 int
227 sys_shmat(l, v, retval)
228 struct lwp *l;
229 void *v;
230 register_t *retval;
231 {
232 struct sys_shmat_args /* {
233 syscallarg(int) shmid;
234 syscallarg(const void *) shmaddr;
235 syscallarg(int) shmflg;
236 } */ *uap = v;
237 struct proc *p = l->l_proc;
238 vaddr_t attach_va;
239 int error;
240
241 error = shmat1(p, SCARG(uap, shmid), SCARG(uap, shmaddr),
242 SCARG(uap, shmflg), &attach_va, 0);
243 if (error != 0)
244 return error;
245 retval[0] = attach_va;
246 return 0;
247 }
248
249 int
250 shmat1(p, shmid, shmaddr, shmflg, attachp, findremoved)
251 struct proc *p;
252 int shmid;
253 const void *shmaddr;
254 int shmflg;
255 vaddr_t *attachp;
256 int findremoved;
257 {
258 int error, i, flags;
259 struct ucred *cred = p->p_ucred;
260 struct shmid_ds *shmseg;
261 struct shmmap_state *shmmap_s = NULL;
262 struct shm_handle *shm_handle;
263 vaddr_t attach_va;
264 vm_prot_t prot;
265 vsize_t size;
266
267 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
268 if (shmmap_s == NULL) {
269 size = shminfo.shmseg * sizeof(struct shmmap_state);
270 shmmap_s = malloc(size, M_SHM, M_WAITOK);
271 for (i = 0; i < shminfo.shmseg; i++)
272 shmmap_s[i].shmid = -1;
273 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
274 }
275 shmseg = shm_find_segment_by_shmid(shmid, findremoved);
276 if (shmseg == NULL)
277 return EINVAL;
278 error = ipcperm(cred, &shmseg->shm_perm,
279 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
280 if (error)
281 return error;
282 for (i = 0; i < shminfo.shmseg; i++) {
283 if (shmmap_s->shmid == -1)
284 break;
285 shmmap_s++;
286 }
287 if (i >= shminfo.shmseg)
288 return EMFILE;
289 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
290 prot = VM_PROT_READ;
291 if ((shmflg & SHM_RDONLY) == 0)
292 prot |= VM_PROT_WRITE;
293 flags = MAP_ANON | MAP_SHARED;
294 if (shmaddr) {
295 flags |= MAP_FIXED;
296 if (shmflg & SHM_RND)
297 attach_va =
298 (vaddr_t)shmaddr & ~(SHMLBA-1);
299 else if (((vaddr_t)shmaddr & (SHMLBA-1)) == 0)
300 attach_va = (vaddr_t)shmaddr;
301 else
302 return EINVAL;
303 } else {
304 /* This is just a hint to vm_mmap() about where to put it. */
305 attach_va =
306 round_page((vaddr_t)p->p_vmspace->vm_taddr +
307 MAXTSIZ + MAXDSIZ);
308 }
309 shm_handle = shmseg->_shm_internal;
310 uao_reference(shm_handle->shm_object);
311 error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
312 shm_handle->shm_object, 0, 0,
313 UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
314 if (error) {
315 return error;
316 }
317 shmmap_s->va = attach_va;
318 shmmap_s->shmid = shmid;
319 shmseg->shm_lpid = p->p_pid;
320 shmseg->shm_atime = time.tv_sec;
321 shmseg->shm_nattch++;
322 *attachp = attach_va;
323 return 0;
324 }
325
326 int
327 sys___shmctl13(l, v, retval)
328 struct lwp *l;
329 void *v;
330 register_t *retval;
331 {
332 struct sys___shmctl13_args /* {
333 syscallarg(int) shmid;
334 syscallarg(int) cmd;
335 syscallarg(struct shmid_ds *) buf;
336 } */ *uap = v;
337 struct proc *p = l->l_proc;
338 struct shmid_ds shmbuf;
339 int cmd, error;
340
341 cmd = SCARG(uap, cmd);
342
343 if (cmd == IPC_SET) {
344 error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
345 if (error)
346 return (error);
347 }
348
349 error = shmctl1(p, SCARG(uap, shmid), cmd,
350 (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
351
352 if (error == 0 && cmd == IPC_STAT)
353 error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
354
355 return (error);
356 }
357
358 int
359 shmctl1(p, shmid, cmd, shmbuf)
360 struct proc *p;
361 int shmid;
362 int cmd;
363 struct shmid_ds *shmbuf;
364 {
365 struct ucred *cred = p->p_ucred;
366 struct shmid_ds *shmseg;
367 int error = 0;
368
369 shmseg = shm_find_segment_by_shmid(shmid, 0);
370 if (shmseg == NULL)
371 return EINVAL;
372 switch (cmd) {
373 case IPC_STAT:
374 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
375 return error;
376 memcpy(shmbuf, shmseg, sizeof(struct shmid_ds));
377 break;
378 case IPC_SET:
379 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
380 return error;
381 shmseg->shm_perm.uid = shmbuf->shm_perm.uid;
382 shmseg->shm_perm.gid = shmbuf->shm_perm.gid;
383 shmseg->shm_perm.mode =
384 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
385 (shmbuf->shm_perm.mode & ACCESSPERMS);
386 shmseg->shm_ctime = time.tv_sec;
387 break;
388 case IPC_RMID:
389 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
390 return error;
391 shmseg->shm_perm._key = IPC_PRIVATE;
392 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
393 if (shmseg->shm_nattch <= 0) {
394 shm_deallocate_segment(shmseg);
395 shm_last_free = IPCID_TO_IX(shmid);
396 }
397 break;
398 case SHM_LOCK:
399 case SHM_UNLOCK:
400 default:
401 return EINVAL;
402 }
403 return 0;
404 }
405
406 static int
407 shmget_existing(p, uap, mode, segnum, retval)
408 struct proc *p;
409 struct sys_shmget_args /* {
410 syscallarg(key_t) key;
411 syscallarg(size_t) size;
412 syscallarg(int) shmflg;
413 } */ *uap;
414 int mode;
415 int segnum;
416 register_t *retval;
417 {
418 struct shmid_ds *shmseg;
419 struct ucred *cred = p->p_ucred;
420 int error;
421
422 shmseg = &shmsegs[segnum];
423 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
424 /*
425 * This segment is in the process of being allocated. Wait
426 * until it's done, and look the key up again (in case the
427 * allocation failed or it was freed).
428 */
429 shmseg->shm_perm.mode |= SHMSEG_WANTED;
430 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
431 if (error)
432 return error;
433 return EAGAIN;
434 }
435 if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
436 return error;
437 if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
438 return EINVAL;
439 if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
440 (IPC_CREAT | IPC_EXCL))
441 return EEXIST;
442 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
443 return 0;
444 }
445
446 static int
447 shmget_allocate_segment(p, uap, mode, retval)
448 struct proc *p;
449 struct sys_shmget_args /* {
450 syscallarg(key_t) key;
451 syscallarg(size_t) size;
452 syscallarg(int) shmflg;
453 } */ *uap;
454 int mode;
455 register_t *retval;
456 {
457 int i, segnum, shmid, size;
458 struct ucred *cred = p->p_ucred;
459 struct shmid_ds *shmseg;
460 struct shm_handle *shm_handle;
461 int error = 0;
462
463 if (SCARG(uap, size) < shminfo.shmmin ||
464 SCARG(uap, size) > shminfo.shmmax)
465 return EINVAL;
466 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
467 return ENOSPC;
468 size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET;
469 if (shm_committed + btoc(size) > shminfo.shmall)
470 return ENOMEM;
471 if (shm_last_free < 0) {
472 for (i = 0; i < shminfo.shmmni; i++)
473 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
474 break;
475 if (i == shminfo.shmmni)
476 panic("shmseg free count inconsistent");
477 segnum = i;
478 } else {
479 segnum = shm_last_free;
480 shm_last_free = -1;
481 }
482 shmseg = &shmsegs[segnum];
483 /*
484 * In case we sleep in malloc(), mark the segment present but deleted
485 * so that noone else tries to create the same key.
486 */
487 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
488 shmseg->shm_perm._key = SCARG(uap, key);
489 shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
490 shm_handle = (struct shm_handle *)
491 malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
492 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
493
494 shm_handle->shm_object = uao_create(size, 0);
495
496 shmseg->_shm_internal = shm_handle;
497 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
498 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
499 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
500 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
501 shmseg->shm_segsz = SCARG(uap, size);
502 shmseg->shm_cpid = p->p_pid;
503 shmseg->shm_lpid = shmseg->shm_nattch = 0;
504 shmseg->shm_atime = shmseg->shm_dtime = 0;
505 shmseg->shm_ctime = time.tv_sec;
506 shm_committed += btoc(size);
507 shm_nused++;
508
509 *retval = shmid;
510 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
511 /*
512 * Somebody else wanted this key while we were asleep. Wake
513 * them up now.
514 */
515 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
516 wakeup((caddr_t)shmseg);
517 }
518 return error;
519 }
520
521 int
522 sys_shmget(l, v, retval)
523 struct lwp *l;
524 void *v;
525 register_t *retval;
526 {
527 struct sys_shmget_args /* {
528 syscallarg(key_t) key;
529 syscallarg(int) size;
530 syscallarg(int) shmflg;
531 } */ *uap = v;
532 struct proc *p = l->l_proc;
533 int segnum, mode, error;
534
535 mode = SCARG(uap, shmflg) & ACCESSPERMS;
536 if (SCARG(uap, key) != IPC_PRIVATE) {
537 again:
538 segnum = shm_find_segment_by_key(SCARG(uap, key));
539 if (segnum >= 0) {
540 error = shmget_existing(p, uap, mode, segnum, retval);
541 if (error == EAGAIN)
542 goto again;
543 return error;
544 }
545 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
546 return ENOENT;
547 }
548 return shmget_allocate_segment(p, uap, mode, retval);
549 }
550
551 void
552 shmfork(vm1, vm2)
553 struct vmspace *vm1, *vm2;
554 {
555 struct shmmap_state *shmmap_s;
556 size_t size;
557 int i;
558
559 if (vm1->vm_shm == NULL) {
560 vm2->vm_shm = NULL;
561 return;
562 }
563
564 size = shminfo.shmseg * sizeof(struct shmmap_state);
565 shmmap_s = malloc(size, M_SHM, M_WAITOK);
566 memcpy(shmmap_s, vm1->vm_shm, size);
567 vm2->vm_shm = (caddr_t)shmmap_s;
568 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
569 if (shmmap_s->shmid != -1)
570 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
571 }
572
573 void
574 shmexit(vm)
575 struct vmspace *vm;
576 {
577 struct shmmap_state *shmmap_s;
578 int i;
579
580 shmmap_s = (struct shmmap_state *)vm->vm_shm;
581 if (shmmap_s == NULL)
582 return;
583 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
584 if (shmmap_s->shmid != -1)
585 shm_delete_mapping(vm, shmmap_s);
586 free(vm->vm_shm, M_SHM);
587 vm->vm_shm = NULL;
588 }
589
590 void
591 shminit()
592 {
593 int i;
594
595 shminfo.shmmax *= PAGE_SIZE;
596
597 for (i = 0; i < shminfo.shmmni; i++) {
598 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
599 shmsegs[i].shm_perm._seq = 0;
600 }
601 shm_last_free = 0;
602 shm_nused = 0;
603 shm_committed = 0;
604 }
605