sysv_shm.c revision 1.64.4.2 1 /* $NetBSD: sysv_shm.c,v 1.64.4.2 2004/02/09 13:16:55 tron 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.64.4.2 2004/02/09 13:16:55 tron 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 #include <uvm/uvm_object.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 = shmseg->_shm_internal;
166 struct uvm_object *uobj = shm_handle->shm_object;
167 size_t size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
168
169 (*uobj->pgops->pgo_detach)(uobj);
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(p, v, retval)
201 struct proc *p;
202 void *v;
203 register_t *retval;
204 {
205 struct sys_shmdt_args /* {
206 syscallarg(const void *) shmaddr;
207 } */ *uap = v;
208 struct shmmap_state *shmmap_s;
209 int i;
210
211 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
212 if (shmmap_s == NULL)
213 return EINVAL;
214
215 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
216 if (shmmap_s->shmid != -1 &&
217 shmmap_s->va == (vaddr_t)SCARG(uap, shmaddr))
218 break;
219 if (i == shminfo.shmseg)
220 return EINVAL;
221 shm_delete_mapping(p->p_vmspace, shmmap_s);
222 return 0;
223 }
224
225 int
226 sys_shmat(p, v, retval)
227 struct proc *p;
228 void *v;
229 register_t *retval;
230 {
231 struct sys_shmat_args /* {
232 syscallarg(int) shmid;
233 syscallarg(const void *) shmaddr;
234 syscallarg(int) shmflg;
235 } */ *uap = v;
236 vaddr_t attach_va;
237 int error;
238
239 error = shmat1(p, SCARG(uap, shmid), SCARG(uap, shmaddr),
240 SCARG(uap, shmflg), &attach_va, 0);
241 if (error != 0)
242 return error;
243 retval[0] = attach_va;
244 return 0;
245 }
246
247 int
248 shmat1(p, shmid, shmaddr, shmflg, attachp, findremoved)
249 struct proc *p;
250 int shmid;
251 const void *shmaddr;
252 int shmflg;
253 vaddr_t *attachp;
254 int findremoved;
255 {
256 int error, i, flags;
257 struct ucred *cred = p->p_ucred;
258 struct shmid_ds *shmseg;
259 struct shmmap_state *shmmap_s = NULL;
260 struct uvm_object *uobj;
261 vaddr_t attach_va;
262 vm_prot_t prot;
263 vsize_t size;
264
265 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
266 if (shmmap_s == NULL) {
267 size = shminfo.shmseg * sizeof(struct shmmap_state);
268 shmmap_s = malloc(size, M_SHM, M_WAITOK);
269 for (i = 0; i < shminfo.shmseg; i++)
270 shmmap_s[i].shmid = -1;
271 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
272 }
273 shmseg = shm_find_segment_by_shmid(shmid, findremoved);
274 if (shmseg == NULL)
275 return EINVAL;
276 error = ipcperm(cred, &shmseg->shm_perm,
277 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
278 if (error)
279 return error;
280 for (i = 0; i < shminfo.shmseg; i++) {
281 if (shmmap_s->shmid == -1)
282 break;
283 shmmap_s++;
284 }
285 if (i >= shminfo.shmseg)
286 return EMFILE;
287 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
288 prot = VM_PROT_READ;
289 if ((shmflg & SHM_RDONLY) == 0)
290 prot |= VM_PROT_WRITE;
291 flags = MAP_ANON | MAP_SHARED;
292 if (shmaddr) {
293 flags |= MAP_FIXED;
294 if (shmflg & SHM_RND)
295 attach_va =
296 (vaddr_t)shmaddr & ~(SHMLBA-1);
297 else if (((vaddr_t)shmaddr & (SHMLBA-1)) == 0)
298 attach_va = (vaddr_t)shmaddr;
299 else
300 return EINVAL;
301 } else {
302 /* This is just a hint to vm_mmap() about where to put it. */
303 attach_va =
304 round_page((vaddr_t)p->p_vmspace->vm_taddr +
305 MAXTSIZ + MAXDSIZ);
306 }
307 uobj = ((struct shm_handle *)shmseg->_shm_internal)->shm_object;
308 (*uobj->pgops->pgo_reference)(uobj);
309 error = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
310 uobj, 0, 0,
311 UVM_MAPFLAG(prot, prot, UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
312 if (error) {
313 (*uobj->pgops->pgo_detach)(uobj);
314 return error;
315 }
316 shmmap_s->va = attach_va;
317 shmmap_s->shmid = shmid;
318 shmseg->shm_lpid = p->p_pid;
319 shmseg->shm_atime = time.tv_sec;
320 shmseg->shm_nattch++;
321 *attachp = attach_va;
322 return 0;
323 }
324
325 int
326 sys___shmctl13(p, v, retval)
327 struct proc *p;
328 void *v;
329 register_t *retval;
330 {
331 struct sys___shmctl13_args /* {
332 syscallarg(int) shmid;
333 syscallarg(int) cmd;
334 syscallarg(struct shmid_ds *) buf;
335 } */ *uap = v;
336 struct shmid_ds shmbuf;
337 int cmd, error;
338
339 cmd = SCARG(uap, cmd);
340
341 if (cmd == IPC_SET) {
342 error = copyin(SCARG(uap, buf), &shmbuf, sizeof(shmbuf));
343 if (error)
344 return (error);
345 }
346
347 error = shmctl1(p, SCARG(uap, shmid), cmd,
348 (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
349
350 if (error == 0 && cmd == IPC_STAT)
351 error = copyout(&shmbuf, SCARG(uap, buf), sizeof(shmbuf));
352
353 return (error);
354 }
355
356 int
357 shmctl1(p, shmid, cmd, shmbuf)
358 struct proc *p;
359 int shmid;
360 int cmd;
361 struct shmid_ds *shmbuf;
362 {
363 struct ucred *cred = p->p_ucred;
364 struct shmid_ds *shmseg;
365 int error = 0;
366
367 shmseg = shm_find_segment_by_shmid(shmid, 0);
368 if (shmseg == NULL)
369 return EINVAL;
370 switch (cmd) {
371 case IPC_STAT:
372 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
373 return error;
374 memcpy(shmbuf, shmseg, sizeof(struct shmid_ds));
375 break;
376 case IPC_SET:
377 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
378 return error;
379 shmseg->shm_perm.uid = shmbuf->shm_perm.uid;
380 shmseg->shm_perm.gid = shmbuf->shm_perm.gid;
381 shmseg->shm_perm.mode =
382 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
383 (shmbuf->shm_perm.mode & ACCESSPERMS);
384 shmseg->shm_ctime = time.tv_sec;
385 break;
386 case IPC_RMID:
387 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
388 return error;
389 shmseg->shm_perm._key = IPC_PRIVATE;
390 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
391 if (shmseg->shm_nattch <= 0) {
392 shm_deallocate_segment(shmseg);
393 shm_last_free = IPCID_TO_IX(shmid);
394 }
395 break;
396 case SHM_LOCK:
397 case SHM_UNLOCK:
398 default:
399 return EINVAL;
400 }
401 return 0;
402 }
403
404 static int
405 shmget_existing(p, uap, mode, segnum, retval)
406 struct proc *p;
407 struct sys_shmget_args /* {
408 syscallarg(key_t) key;
409 syscallarg(size_t) size;
410 syscallarg(int) shmflg;
411 } */ *uap;
412 int mode;
413 int segnum;
414 register_t *retval;
415 {
416 struct shmid_ds *shmseg;
417 struct ucred *cred = p->p_ucred;
418 int error;
419
420 shmseg = &shmsegs[segnum];
421 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
422 /*
423 * This segment is in the process of being allocated. Wait
424 * until it's done, and look the key up again (in case the
425 * allocation failed or it was freed).
426 */
427 shmseg->shm_perm.mode |= SHMSEG_WANTED;
428 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
429 if (error)
430 return error;
431 return EAGAIN;
432 }
433 if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
434 return error;
435 if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
436 return EINVAL;
437 if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
438 (IPC_CREAT | IPC_EXCL))
439 return EEXIST;
440 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
441 return 0;
442 }
443
444 static int
445 shmget_allocate_segment(p, uap, mode, retval)
446 struct proc *p;
447 struct sys_shmget_args /* {
448 syscallarg(key_t) key;
449 syscallarg(size_t) size;
450 syscallarg(int) shmflg;
451 } */ *uap;
452 int mode;
453 register_t *retval;
454 {
455 int i, segnum, shmid, size;
456 struct ucred *cred = p->p_ucred;
457 struct shmid_ds *shmseg;
458 struct shm_handle *shm_handle;
459 int error = 0;
460
461 if (SCARG(uap, size) < shminfo.shmmin ||
462 SCARG(uap, size) > shminfo.shmmax)
463 return EINVAL;
464 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
465 return ENOSPC;
466 size = (SCARG(uap, size) + PGOFSET) & ~PGOFSET;
467 if (shm_committed + btoc(size) > shminfo.shmall)
468 return ENOMEM;
469 if (shm_last_free < 0) {
470 for (i = 0; i < shminfo.shmmni; i++)
471 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
472 break;
473 if (i == shminfo.shmmni)
474 panic("shmseg free count inconsistent");
475 segnum = i;
476 } else {
477 segnum = shm_last_free;
478 shm_last_free = -1;
479 }
480 shmseg = &shmsegs[segnum];
481 /*
482 * In case we sleep in malloc(), mark the segment present but deleted
483 * so that noone else tries to create the same key.
484 */
485 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
486 shmseg->shm_perm._key = SCARG(uap, key);
487 shmseg->shm_perm._seq = (shmseg->shm_perm._seq + 1) & 0x7fff;
488 shm_handle = (struct shm_handle *)
489 malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
490 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
491
492 shm_handle->shm_object = uao_create(size, 0);
493
494 shmseg->_shm_internal = shm_handle;
495 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
496 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
497 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
498 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
499 shmseg->shm_segsz = SCARG(uap, size);
500 shmseg->shm_cpid = p->p_pid;
501 shmseg->shm_lpid = shmseg->shm_nattch = 0;
502 shmseg->shm_atime = shmseg->shm_dtime = 0;
503 shmseg->shm_ctime = time.tv_sec;
504 shm_committed += btoc(size);
505 shm_nused++;
506
507 *retval = shmid;
508 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
509 /*
510 * Somebody else wanted this key while we were asleep. Wake
511 * them up now.
512 */
513 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
514 wakeup((caddr_t)shmseg);
515 }
516 return error;
517 }
518
519 int
520 sys_shmget(p, v, retval)
521 struct proc *p;
522 void *v;
523 register_t *retval;
524 {
525 struct sys_shmget_args /* {
526 syscallarg(key_t) key;
527 syscallarg(int) size;
528 syscallarg(int) shmflg;
529 } */ *uap = v;
530 int segnum, mode, error;
531
532 mode = SCARG(uap, shmflg) & ACCESSPERMS;
533 if (SCARG(uap, key) != IPC_PRIVATE) {
534 again:
535 segnum = shm_find_segment_by_key(SCARG(uap, key));
536 if (segnum >= 0) {
537 error = shmget_existing(p, uap, mode, segnum, retval);
538 if (error == EAGAIN)
539 goto again;
540 return error;
541 }
542 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
543 return ENOENT;
544 }
545 return shmget_allocate_segment(p, uap, mode, retval);
546 }
547
548 void
549 shmfork(vm1, vm2)
550 struct vmspace *vm1, *vm2;
551 {
552 struct shmmap_state *shmmap_s;
553 size_t size;
554 int i;
555
556 if (vm1->vm_shm == NULL) {
557 vm2->vm_shm = NULL;
558 return;
559 }
560
561 size = shminfo.shmseg * sizeof(struct shmmap_state);
562 shmmap_s = malloc(size, M_SHM, M_WAITOK);
563 memcpy(shmmap_s, vm1->vm_shm, size);
564 vm2->vm_shm = (caddr_t)shmmap_s;
565 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
566 if (shmmap_s->shmid != -1)
567 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
568 }
569
570 void
571 shmexit(vm)
572 struct vmspace *vm;
573 {
574 struct shmmap_state *shmmap_s;
575 int i;
576
577 shmmap_s = (struct shmmap_state *)vm->vm_shm;
578 if (shmmap_s == NULL)
579 return;
580 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
581 if (shmmap_s->shmid != -1)
582 shm_delete_mapping(vm, shmmap_s);
583 free(vm->vm_shm, M_SHM);
584 vm->vm_shm = NULL;
585 }
586
587 void
588 shminit()
589 {
590 int i;
591
592 shminfo.shmmax *= PAGE_SIZE;
593
594 for (i = 0; i < shminfo.shmmni; i++) {
595 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
596 shmsegs[i].shm_perm._seq = 0;
597 }
598 shm_last_free = 0;
599 shm_nused = 0;
600 shm_committed = 0;
601 }
602