sysv_shm.c revision 1.60 1 /* $NetBSD: sysv_shm.c,v 1.60 2000/11/14 22:16:38 thorpej 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(p, v, retval)
199 struct proc *p;
200 void *v;
201 register_t *retval;
202 {
203 struct sys_shmdt_args /* {
204 syscallarg(const void *) shmaddr;
205 } */ *uap = v;
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 return shm_delete_mapping(p->p_vmspace, shmmap_s);
220 }
221
222 int
223 sys_shmat(p, v, retval)
224 struct proc *p;
225 void *v;
226 register_t *retval;
227 {
228 struct sys_shmat_args /* {
229 syscallarg(int) shmid;
230 syscallarg(const void *) shmaddr;
231 syscallarg(int) shmflg;
232 } */ *uap = v;
233 int error, i, flags;
234 struct ucred *cred = p->p_ucred;
235 struct shmid_ds *shmseg;
236 struct shmmap_state *shmmap_s = NULL;
237 struct shm_handle *shm_handle;
238 vaddr_t attach_va;
239 vm_prot_t prot;
240 vsize_t size;
241 int rv;
242
243 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
244 if (shmmap_s == NULL) {
245 size = shminfo.shmseg * sizeof(struct shmmap_state);
246 shmmap_s = malloc(size, M_SHM, M_WAITOK);
247 for (i = 0; i < shminfo.shmseg; i++)
248 shmmap_s[i].shmid = -1;
249 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
250 }
251 shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
252 if (shmseg == NULL)
253 return EINVAL;
254 error = ipcperm(cred, &shmseg->shm_perm,
255 (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
256 if (error)
257 return error;
258 for (i = 0; i < shminfo.shmseg; i++) {
259 if (shmmap_s->shmid == -1)
260 break;
261 shmmap_s++;
262 }
263 if (i >= shminfo.shmseg)
264 return EMFILE;
265 size = (shmseg->shm_segsz + PGOFSET) & ~PGOFSET;
266 prot = VM_PROT_READ;
267 if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
268 prot |= VM_PROT_WRITE;
269 flags = MAP_ANON | MAP_SHARED;
270 if (SCARG(uap, shmaddr)) {
271 flags |= MAP_FIXED;
272 if (SCARG(uap, shmflg) & SHM_RND)
273 attach_va =
274 (vaddr_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
275 else if (((vaddr_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
276 attach_va = (vaddr_t)SCARG(uap, shmaddr);
277 else
278 return EINVAL;
279 } else {
280 /* This is just a hint to vm_mmap() about where to put it. */
281 attach_va =
282 round_page((vaddr_t)p->p_vmspace->vm_taddr +
283 MAXTSIZ + MAXDSIZ);
284 }
285 shm_handle = shmseg->_shm_internal;
286 uao_reference(shm_handle->shm_object);
287 rv = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
288 shm_handle->shm_object, 0, 0,
289 UVM_MAPFLAG(prot, prot, UVM_INH_SHARE,
290 UVM_ADV_RANDOM, 0));
291 if (rv != KERN_SUCCESS) {
292 return ENOMEM;
293 }
294
295 shmmap_s->va = attach_va;
296 shmmap_s->shmid = SCARG(uap, shmid);
297 shmseg->shm_lpid = p->p_pid;
298 shmseg->shm_atime = time.tv_sec;
299 shmseg->shm_nattch++;
300 *retval = attach_va;
301 return 0;
302 }
303
304 int
305 sys___shmctl13(p, v, retval)
306 struct proc *p;
307 void *v;
308 register_t *retval;
309 {
310 struct sys___shmctl13_args /* {
311 syscallarg(int) shmid;
312 syscallarg(int) cmd;
313 syscallarg(struct shmid_ds *) buf;
314 } */ *uap = v;
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(p, v, retval)
500 struct proc *p;
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 int segnum, mode, error;
510
511 mode = SCARG(uap, shmflg) & ACCESSPERMS;
512 if (SCARG(uap, key) != IPC_PRIVATE) {
513 again:
514 segnum = shm_find_segment_by_key(SCARG(uap, key));
515 if (segnum >= 0) {
516 error = shmget_existing(p, uap, mode, segnum, retval);
517 if (error == EAGAIN)
518 goto again;
519 return error;
520 }
521 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
522 return ENOENT;
523 }
524 return shmget_allocate_segment(p, uap, mode, retval);
525 }
526
527 void
528 shmfork(vm1, vm2)
529 struct vmspace *vm1, *vm2;
530 {
531 struct shmmap_state *shmmap_s;
532 size_t size;
533 int i;
534
535 if (vm1->vm_shm == NULL) {
536 vm2->vm_shm = NULL;
537 return;
538 }
539
540 size = shminfo.shmseg * sizeof(struct shmmap_state);
541 shmmap_s = malloc(size, M_SHM, M_WAITOK);
542 memcpy(shmmap_s, vm1->vm_shm, size);
543 vm2->vm_shm = (caddr_t)shmmap_s;
544 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
545 if (shmmap_s->shmid != -1)
546 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
547 }
548
549 void
550 shmexit(vm)
551 struct vmspace *vm;
552 {
553 struct shmmap_state *shmmap_s;
554 int i;
555
556 shmmap_s = (struct shmmap_state *)vm->vm_shm;
557 if (shmmap_s == NULL)
558 return;
559 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
560 if (shmmap_s->shmid != -1)
561 shm_delete_mapping(vm, shmmap_s);
562 free(vm->vm_shm, M_SHM);
563 vm->vm_shm = NULL;
564 }
565
566 void
567 shminit()
568 {
569 int i;
570
571 shminfo.shmmax *= PAGE_SIZE;
572
573 for (i = 0; i < shminfo.shmmni; i++) {
574 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
575 shmsegs[i].shm_perm._seq = 0;
576 }
577 shm_last_free = 0;
578 shm_nused = 0;
579 shm_committed = 0;
580 }
581