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