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