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