sysv_shm.c revision 1.60.2.6 1 /* $NetBSD: sysv_shm.c,v 1.60.2.6 2002/05/29 21:33:17 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.6 2002/05/29 21:33:17 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/sa.h>
84 #include <sys/syscallargs.h>
85
86 #include <uvm/uvm_extern.h>
87
88 struct shmid_ds *shm_find_segment_by_shmid __P((int, int));
89
90 /*
91 * Provides the following externally accessible functions:
92 *
93 * shminit(void); initialization
94 * shmexit(struct vmspace *) cleanup
95 * shmfork(struct vmspace *, struct vmspace *) fork handling
96 *
97 * Structures:
98 * shmsegs (an array of 'struct shmid_ds')
99 * per proc array of 'struct shmmap_state'
100 */
101
102 #define SHMSEG_FREE 0x0200
103 #define SHMSEG_REMOVED 0x0400
104 #define SHMSEG_ALLOCATED 0x0800
105 #define SHMSEG_WANTED 0x1000
106
107 int shm_last_free, shm_nused, shm_committed;
108 struct shmid_ds *shmsegs;
109
110 struct shm_handle {
111 struct uvm_object *shm_object;
112 };
113
114 struct shmmap_state {
115 vaddr_t va;
116 int shmid;
117 };
118
119 static int shm_find_segment_by_key __P((key_t));
120 static void shm_deallocate_segment __P((struct shmid_ds *));
121 static void shm_delete_mapping __P((struct vmspace *, struct shmmap_state *));
122 static int shmget_existing __P((struct proc *, struct sys_shmget_args *,
123 int, int, register_t *));
124 static int shmget_allocate_segment __P((struct proc *, struct sys_shmget_args *,
125 int, register_t *));
126
127 static int
128 shm_find_segment_by_key(key)
129 key_t key;
130 {
131 int i;
132
133 for (i = 0; i < shminfo.shmmni; i++)
134 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
135 shmsegs[i].shm_perm._key == key)
136 return i;
137 return -1;
138 }
139
140 struct shmid_ds *
141 shm_find_segment_by_shmid(shmid, findremoved)
142 int shmid;
143 int findremoved;
144 {
145 int segnum;
146 struct shmid_ds *shmseg;
147
148 segnum = IPCID_TO_IX(shmid);
149 if (segnum < 0 || segnum >= shminfo.shmmni)
150 return NULL;
151 shmseg = &shmsegs[segnum];
152 if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0)
153 return NULL;
154 if (!findremoved && ((shmseg->shm_perm.mode & SHMSEG_REMOVED) != 0))
155 return NULL;
156 if (shmseg->shm_perm._seq != IPCID_TO_SEQ(shmid))
157 return NULL;
158 return shmseg;
159 }
160
161 static void
162 shm_deallocate_segment(shmseg)
163 struct shmid_ds *shmseg;
164 {
165 struct shm_handle *shm_handle;
166 size_t size;
167
168 shm_handle = shmseg->_shm_internal;
169 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
170 uao_detach(shm_handle->shm_object);
171 free((caddr_t)shm_handle, M_SHM);
172 shmseg->_shm_internal = NULL;
173 shm_committed -= btoc(size);
174 shmseg->shm_perm.mode = SHMSEG_FREE;
175 shm_nused--;
176 }
177
178 static void
179 shm_delete_mapping(vm, shmmap_s)
180 struct vmspace *vm;
181 struct shmmap_state *shmmap_s;
182 {
183 struct shmid_ds *shmseg;
184 int segnum;
185 size_t size;
186
187 segnum = IPCID_TO_IX(shmmap_s->shmid);
188 shmseg = &shmsegs[segnum];
189 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
190 uvm_deallocate(&vm->vm_map, shmmap_s->va, size);
191 shmmap_s->shmid = -1;
192 shmseg->shm_dtime = time.tv_sec;
193 if ((--shmseg->shm_nattch <= 0) &&
194 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
195 shm_deallocate_segment(shmseg);
196 shm_last_free = segnum;
197 }
198 }
199
200 int
201 sys_shmdt(l, v, retval)
202 struct lwp *l;
203 void *v;
204 register_t *retval;
205 {
206 struct sys_shmdt_args /* {
207 syscallarg(const void *) shmaddr;
208 } */ *uap = v;
209 struct proc *p = l->l_proc;
210 struct shmmap_state *shmmap_s;
211 int i;
212
213 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
214 if (shmmap_s == NULL)
215 return EINVAL;
216
217 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
218 if (shmmap_s->shmid != -1 &&
219 shmmap_s->va == (vaddr_t)SCARG(uap, shmaddr))
220 break;
221 if (i == shminfo.shmseg)
222 return EINVAL;
223 shm_delete_mapping(p->p_vmspace, shmmap_s);
224 return 0;
225 }
226
227 int
228 sys_shmat(l, v, retval)
229 struct lwp *l;
230 void *v;
231 register_t *retval;
232 {
233 struct sys_shmat_args /* {
234 syscallarg(int) shmid;
235 syscallarg(const void *) shmaddr;
236 syscallarg(int) shmflg;
237 } */ *uap = v;
238 struct proc *p = l->l_proc;
239 vaddr_t attach_va;
240 int error;
241
242 error = shmat1(p, SCARG(uap, shmid), SCARG(uap, shmaddr),
243 SCARG(uap, shmflg), &attach_va, 0);
244 if (error != 0)
245 return error;
246 retval[0] = attach_va;
247 return 0;
248 }
249
250 int
251 shmat1(p, shmid, shmaddr, shmflg, attachp, findremoved)
252 struct proc *p;
253 int shmid;
254 const void *shmaddr;
255 int shmflg;
256 vaddr_t *attachp;
257 int findremoved;
258 {
259 int error, i, flags;
260 struct ucred *cred = p->p_ucred;
261 struct shmid_ds *shmseg;
262 struct shmmap_state *shmmap_s = NULL;
263 struct shm_handle *shm_handle;
264 vaddr_t attach_va;
265 vm_prot_t prot;
266 vsize_t size;
267
268 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
269 if (shmmap_s == NULL) {
270 size = shminfo.shmseg * sizeof(struct shmmap_state);
271 shmmap_s = malloc(size, M_SHM, M_WAITOK);
272 for (i = 0; i < shminfo.shmseg; i++)
273 shmmap_s[i].shmid = -1;
274 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
275 }
276 shmseg = shm_find_segment_by_shmid(shmid, findremoved);
277 if (shmseg == NULL)
278 return EINVAL;
279 error = ipcperm(cred, &shmseg->shm_perm,
280 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
281 if (error)
282 return error;
283 for (i = 0; i < shminfo.shmseg; i++) {
284 if (shmmap_s->shmid == -1)
285 break;
286 shmmap_s++;
287 }
288 if (i >= shminfo.shmseg)
289 return EMFILE;
290 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
291 prot = VM_PROT_READ;
292 if ((shmflg & SHM_RDONLY) == 0)
293 prot |= VM_PROT_WRITE;
294 flags = MAP_ANON | MAP_SHARED;
295 if (shmaddr) {
296 flags |= MAP_FIXED;
297 if (shmflg & SHM_RND)
298 attach_va =
299 (vaddr_t)shmaddr & ~(SHMLBA-1);
300 else if (((vaddr_t)shmaddr & (SHMLBA-1)) == 0)
301 attach_va = (vaddr_t)shmaddr;
302 else
303 return EINVAL;
304 } else {
305 /* This is just a hint to vm_mmap() about where to put it. */
306 attach_va =
307 round_page((vaddr_t)p->p_vmspace->vm_taddr +
308 MAXTSIZ + MAXDSIZ);
309 }
310 shm_handle = shmseg->_shm_internal;
311 uao_reference(shm_handle->shm_object);
312 error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
313 shm_handle->shm_object, 0, 0,
314 UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
315 if (error) {
316 return error;
317 }
318 shmmap_s->va = attach_va;
319 shmmap_s->shmid = shmid;
320 shmseg->shm_lpid = p->p_pid;
321 shmseg->shm_atime = time.tv_sec;
322 shmseg->shm_nattch++;
323 *attachp = attach_va;
324 return 0;
325 }
326
327 int
328 sys___shmctl13(l, v, retval)
329 struct lwp *l;
330 void *v;
331 register_t *retval;
332 {
333 struct sys___shmctl13_args /* {
334 syscallarg(int) shmid;
335 syscallarg(int) cmd;
336 syscallarg(struct shmid_ds *) buf;
337 } */ *uap = v;
338 struct proc *p = l->l_proc;
339 struct shmid_ds shmbuf;
340 int cmd, error;
341
342 cmd = SCARG(uap, cmd);
343
344 if (cmd == IPC_SET) {
345 error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
346 if (error)
347 return (error);
348 }
349
350 error = shmctl1(p, SCARG(uap, shmid), cmd,
351 (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
352
353 if (error == 0 && cmd == IPC_STAT)
354 error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
355
356 return (error);
357 }
358
359 int
360 shmctl1(p, shmid, cmd, shmbuf)
361 struct proc *p;
362 int shmid;
363 int cmd;
364 struct shmid_ds *shmbuf;
365 {
366 struct ucred *cred = p->p_ucred;
367 struct shmid_ds *shmseg;
368 int error = 0;
369
370 shmseg = shm_find_segment_by_shmid(shmid, 0);
371 if (shmseg == NULL)
372 return EINVAL;
373 switch (cmd) {
374 case IPC_STAT:
375 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
376 return error;
377 memcpy(shmbuf, shmseg, sizeof(struct shmid_ds));
378 break;
379 case IPC_SET:
380 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
381 return error;
382 shmseg->shm_perm.uid = shmbuf->shm_perm.uid;
383 shmseg->shm_perm.gid = shmbuf->shm_perm.gid;
384 shmseg->shm_perm.mode =
385 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
386 (shmbuf->shm_perm.mode & ACCESSPERMS);
387 shmseg->shm_ctime = time.tv_sec;
388 break;
389 case IPC_RMID:
390 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
391 return error;
392 shmseg->shm_perm._key = IPC_PRIVATE;
393 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
394 if (shmseg->shm_nattch <= 0) {
395 shm_deallocate_segment(shmseg);
396 shm_last_free = IPCID_TO_IX(shmid);
397 }
398 break;
399 case SHM_LOCK:
400 case SHM_UNLOCK:
401 default:
402 return EINVAL;
403 }
404 return 0;
405 }
406
407 static int
408 shmget_existing(p, uap, mode, segnum, retval)
409 struct proc *p;
410 struct sys_shmget_args /* {
411 syscallarg(key_t) key;
412 syscallarg(size_t) size;
413 syscallarg(int) shmflg;
414 } */ *uap;
415 int mode;
416 int segnum;
417 register_t *retval;
418 {
419 struct shmid_ds *shmseg;
420 struct ucred *cred = p->p_ucred;
421 int error;
422
423 shmseg = &shmsegs[segnum];
424 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
425 /*
426 * This segment is in the process of being allocated. Wait
427 * until it's done, and look the key up again (in case the
428 * allocation failed or it was freed).
429 */
430 shmseg->shm_perm.mode |= SHMSEG_WANTED;
431 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
432 if (error)
433 return error;
434 return EAGAIN;
435 }
436 if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
437 return error;
438 if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
439 return EINVAL;
440 if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
441 (IPC_CREAT | IPC_EXCL))
442 return EEXIST;
443 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
444 return 0;
445 }
446
447 static int
448 shmget_allocate_segment(p, uap, mode, retval)
449 struct proc *p;
450 struct sys_shmget_args /* {
451 syscallarg(key_t) key;
452 syscallarg(size_t) size;
453 syscallarg(int) shmflg;
454 } */ *uap;
455 int mode;
456 register_t *retval;
457 {
458 int i, segnum, shmid, size;
459 struct ucred *cred = p->p_ucred;
460 struct shmid_ds *shmseg;
461 struct shm_handle *shm_handle;
462 int error = 0;
463
464 if (SCARG(uap, size) < shminfo.shmmin ||
465 SCARG(uap, size) > shminfo.shmmax)
466 return EINVAL;
467 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
468 return ENOSPC;
469 size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET;
470 if (shm_committed + btoc(size) > shminfo.shmall)
471 return ENOMEM;
472 if (shm_last_free < 0) {
473 for (i = 0; i < shminfo.shmmni; i++)
474 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
475 break;
476 if (i == shminfo.shmmni)
477 panic("shmseg free count inconsistent");
478 segnum = i;
479 } else {
480 segnum = shm_last_free;
481 shm_last_free = -1;
482 }
483 shmseg = &shmsegs[segnum];
484 /*
485 * In case we sleep in malloc(), mark the segment present but deleted
486 * so that noone else tries to create the same key.
487 */
488 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
489 shmseg->shm_perm._key = SCARG(uap, key);
490 shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
491 shm_handle = (struct shm_handle *)
492 malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
493 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
494
495 shm_handle->shm_object = uao_create(size, 0);
496
497 shmseg->_shm_internal = shm_handle;
498 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
499 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
500 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
501 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
502 shmseg->shm_segsz = SCARG(uap, size);
503 shmseg->shm_cpid = p->p_pid;
504 shmseg->shm_lpid = shmseg->shm_nattch = 0;
505 shmseg->shm_atime = shmseg->shm_dtime = 0;
506 shmseg->shm_ctime = time.tv_sec;
507 shm_committed += btoc(size);
508 shm_nused++;
509
510 *retval = shmid;
511 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
512 /*
513 * Somebody else wanted this key while we were asleep. Wake
514 * them up now.
515 */
516 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
517 wakeup((caddr_t)shmseg);
518 }
519 return error;
520 }
521
522 int
523 sys_shmget(l, v, retval)
524 struct lwp *l;
525 void *v;
526 register_t *retval;
527 {
528 struct sys_shmget_args /* {
529 syscallarg(key_t) key;
530 syscallarg(int) size;
531 syscallarg(int) shmflg;
532 } */ *uap = v;
533 struct proc *p = l->l_proc;
534 int segnum, mode, error;
535
536 mode = SCARG(uap, shmflg) & ACCESSPERMS;
537 if (SCARG(uap, key) != IPC_PRIVATE) {
538 again:
539 segnum = shm_find_segment_by_key(SCARG(uap, key));
540 if (segnum >= 0) {
541 error = shmget_existing(p, uap, mode, segnum, retval);
542 if (error == EAGAIN)
543 goto again;
544 return error;
545 }
546 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
547 return ENOENT;
548 }
549 return shmget_allocate_segment(p, uap, mode, retval);
550 }
551
552 void
553 shmfork(vm1, vm2)
554 struct vmspace *vm1, *vm2;
555 {
556 struct shmmap_state *shmmap_s;
557 size_t size;
558 int i;
559
560 if (vm1->vm_shm == NULL) {
561 vm2->vm_shm = NULL;
562 return;
563 }
564
565 size = shminfo.shmseg * sizeof(struct shmmap_state);
566 shmmap_s = malloc(size, M_SHM, M_WAITOK);
567 memcpy(shmmap_s, vm1->vm_shm, size);
568 vm2->vm_shm = (caddr_t)shmmap_s;
569 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
570 if (shmmap_s->shmid != -1)
571 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
572 }
573
574 void
575 shmexit(vm)
576 struct vmspace *vm;
577 {
578 struct shmmap_state *shmmap_s;
579 int i;
580
581 shmmap_s = (struct shmmap_state *)vm->vm_shm;
582 if (shmmap_s == NULL)
583 return;
584 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
585 if (shmmap_s->shmid != -1)
586 shm_delete_mapping(vm, shmmap_s);
587 free(vm->vm_shm, M_SHM);
588 vm->vm_shm = NULL;
589 }
590
591 void
592 shminit()
593 {
594 int i;
595
596 shminfo.shmmax *= PAGE_SIZE;
597
598 for (i = 0; i < shminfo.shmmni; i++) {
599 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
600 shmsegs[i].shm_perm._seq = 0;
601 }
602 shm_last_free = 0;
603 shm_nused = 0;
604 shm_committed = 0;
605 }
606