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