sysv_shm.c revision 1.26 1 /* $NetBSD: sysv_shm.c,v 1.26 1994/10/20 04:23:19 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Adam Glass and Charles
17 * Hannum.
18 * 4. The names of the authors may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/shm.h>
37 #include <sys/proc.h>
38 #include <sys/uio.h>
39 #include <sys/time.h>
40 #include <sys/malloc.h>
41 #include <sys/mman.h>
42 #include <sys/systm.h>
43 #include <sys/stat.h>
44
45 #include <sys/mount.h>
46 #include <sys/syscallargs.h>
47
48 #include <vm/vm.h>
49 #include <vm/vm_map.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_kern.h>
52
53 /*
54 * Provides the following externally accessible functions:
55 *
56 * shminit(void); initialization
57 * shmexit(struct proc *) cleanup
58 * shmfork(struct proc *, struct proc *, int) fork handling
59 * shmsys(arg1, arg2, arg3, arg4); shm{at,ctl,dt,get}(arg2, arg3, arg4)
60 *
61 * Structures:
62 * shmsegs (an array of 'struct shmid_ds')
63 * per proc array of 'struct shmmap_state'
64 */
65
66 #define SHMSEG_FREE 0x0200
67 #define SHMSEG_REMOVED 0x0400
68 #define SHMSEG_ALLOCATED 0x0800
69 #define SHMSEG_WANTED 0x1000
70
71 vm_map_t sysvshm_map;
72 int shm_last_free, shm_nused, shm_committed;
73
74 struct shm_handle {
75 vm_offset_t kva;
76 };
77
78 struct shmmap_state {
79 vm_offset_t va;
80 int shmid;
81 };
82
83 static void shm_deallocate_segment __P((struct shmid_ds *));
84 static int shm_find_segment_by_key __P((key_t));
85 static struct shmid_ds *shm_find_segment_by_shmid __P((int));
86 static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
87
88 static int
89 shm_find_segment_by_key(key)
90 key_t key;
91 {
92 int i;
93
94 for (i = 0; i < shminfo.shmmni; i++)
95 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
96 shmsegs[i].shm_perm.key == key)
97 return i;
98 return -1;
99 }
100
101 static struct shmid_ds *
102 shm_find_segment_by_shmid(shmid)
103 int shmid;
104 {
105 int segnum;
106 struct shmid_ds *shmseg;
107
108 segnum = IPCID_TO_IX(shmid);
109 if (segnum < 0 || segnum >= shminfo.shmmni)
110 return NULL;
111 shmseg = &shmsegs[segnum];
112 if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
113 != SHMSEG_ALLOCATED ||
114 shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
115 return NULL;
116 return shmseg;
117 }
118
119 static void
120 shm_deallocate_segment(shmseg)
121 struct shmid_ds *shmseg;
122 {
123 struct shm_handle *shm_handle;
124 size_t size;
125
126 shm_handle = shmseg->shm_internal;
127 size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
128 vm_deallocate(sysvshm_map, shm_handle->kva, size);
129 free((caddr_t)shm_handle, M_SHM);
130 shmseg->shm_internal = NULL;
131 shm_committed -= btoc(size);
132 shmseg->shm_perm.mode = SHMSEG_FREE;
133 shm_nused--;
134 }
135
136 static int
137 shm_delete_mapping(p, shmmap_s)
138 struct proc *p;
139 struct shmmap_state *shmmap_s;
140 {
141 struct shmid_ds *shmseg;
142 int segnum, result;
143 size_t size;
144
145 segnum = IPCID_TO_IX(shmmap_s->shmid);
146 shmseg = &shmsegs[segnum];
147 size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
148 result = vm_deallocate(&p->p_vmspace->vm_map, shmmap_s->va, size);
149 if (result != KERN_SUCCESS)
150 return EINVAL;
151 shmmap_s->shmid = -1;
152 shmseg->shm_dtime = time.tv_sec;
153 if ((--shmseg->shm_nattch <= 0) &&
154 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
155 shm_deallocate_segment(shmseg);
156 shm_last_free = segnum;
157 }
158 return 0;
159 }
160
161 int
162 shmdt(p, uap, retval)
163 struct proc *p;
164 struct shmdt_args /* {
165 syscallarg(void *) shmaddr;
166 } */ *uap;
167 register_t *retval;
168 {
169 struct shmmap_state *shmmap_s;
170 int i;
171
172 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
173 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
174 if (shmmap_s->shmid != -1 &&
175 shmmap_s->va == (vm_offset_t)SCARG(uap, shmaddr))
176 break;
177 if (i == shminfo.shmseg)
178 return EINVAL;
179 return shm_delete_mapping(p, shmmap_s);
180 }
181
182 int
183 shmat(p, uap, retval)
184 struct proc *p;
185 struct shmat_args /* {
186 syscallarg(int) shmid;
187 syscallarg(void *) shmaddr;
188 syscallarg(int) shmflag;
189 } */ *uap;
190 register_t *retval;
191 {
192 int error, i, flags;
193 struct ucred *cred = p->p_ucred;
194 struct shmid_ds *shmseg;
195 struct shmmap_state *shmmap_s = NULL;
196 vm_offset_t attach_va;
197 vm_prot_t prot;
198 vm_size_t size;
199
200 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
201 if (shmmap_s == NULL) {
202 size = shminfo.shmseg * sizeof(struct shmmap_state);
203 shmmap_s = malloc(size, M_SHM, M_WAITOK);
204 for (i = 0; i < shminfo.shmseg; i++)
205 shmmap_s[i].shmid = -1;
206 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
207 }
208 shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
209 if (shmseg == NULL)
210 return EINVAL;
211 if (error = ipcperm(cred, &shmseg->shm_perm,
212 (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W))
213 return error;
214 for (i = 0; i < shminfo.shmseg; i++) {
215 if (shmmap_s->shmid == -1)
216 break;
217 shmmap_s++;
218 }
219 if (i >= shminfo.shmseg)
220 return EMFILE;
221 size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
222 prot = VM_PROT_READ;
223 if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
224 prot |= VM_PROT_WRITE;
225 flags = MAP_ANON | MAP_SHARED;
226 if (SCARG(uap, shmaddr)) {
227 flags |= MAP_FIXED;
228 if (SCARG(uap, shmflg) & SHM_RND)
229 attach_va =
230 (vm_offset_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
231 else if (((vm_offset_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
232 attach_va = (vm_offset_t)SCARG(uap, shmaddr);
233 else
234 return EINVAL;
235 } else {
236 /* This is just a hint to vm_mmap() about where to put it. */
237 attach_va = round_page(p->p_vmspace->vm_daddr + MAXDSIZ);
238 }
239 error = vm_mmap(&p->p_vmspace->vm_map, &attach_va, size, prot,
240 VM_PROT_DEFAULT, flags, (caddr_t)(long)SCARG(uap, shmid), 0);
241 if (error)
242 return error;
243 shmmap_s->va = attach_va;
244 shmmap_s->shmid = SCARG(uap, shmid);
245 shmseg->shm_lpid = p->p_pid;
246 shmseg->shm_atime = time.tv_sec;
247 shmseg->shm_nattch++;
248 *retval = attach_va;
249 return 0;
250 }
251
252 int
253 shmctl(p, uap, retval)
254 struct proc *p;
255 struct shmctl_args /* {
256 syscallarg(int) shmid;
257 syscallarg(int) cmd;
258 syscallarg(struct shmid_ds *) buf;
259 } */ *uap;
260 register_t *retval;
261 {
262 int error, segnum;
263 struct ucred *cred = p->p_ucred;
264 struct shmid_ds inbuf;
265 struct shmid_ds *shmseg;
266
267 shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
268 if (shmseg == NULL)
269 return EINVAL;
270 switch (SCARG(uap, cmd)) {
271 case IPC_STAT:
272 if (error = ipcperm(cred, &shmseg->shm_perm, IPC_R))
273 return error;
274 if (error = copyout((caddr_t)shmseg, SCARG(uap, buf),
275 sizeof(inbuf)))
276 return error;
277 break;
278 case IPC_SET:
279 if (error = ipcperm(cred, &shmseg->shm_perm, IPC_M))
280 return error;
281 if (error = copyin(SCARG(uap, buf), (caddr_t)&inbuf,
282 sizeof(inbuf)))
283 return error;
284 shmseg->shm_perm.uid = inbuf.shm_perm.uid;
285 shmseg->shm_perm.gid = inbuf.shm_perm.gid;
286 shmseg->shm_perm.mode =
287 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
288 (inbuf.shm_perm.mode & ACCESSPERMS);
289 shmseg->shm_ctime = time.tv_sec;
290 break;
291 case IPC_RMID:
292 if (error = ipcperm(cred, &shmseg->shm_perm, IPC_M))
293 return error;
294 shmseg->shm_perm.key = IPC_PRIVATE;
295 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
296 if (shmseg->shm_nattch <= 0) {
297 shm_deallocate_segment(shmseg);
298 shm_last_free = IPCID_TO_IX(SCARG(uap, shmid));
299 }
300 break;
301 #if 0
302 case SHM_LOCK:
303 case SHM_UNLOCK:
304 #endif
305 default:
306 return EINVAL;
307 }
308 return 0;
309 }
310
311 static int
312 shmget_existing(p, uap, mode, segnum, retval)
313 struct proc *p;
314 struct shmget_args /* {
315 syscallarg(key_t) key;
316 syscallarg(int) size;
317 syscallarg(int) shmflag;
318 } */ *uap;
319 int mode;
320 int segnum;
321 register_t *retval;
322 {
323 struct shmid_ds *shmseg;
324 struct ucred *cred = p->p_ucred;
325 int error;
326
327 shmseg = &shmsegs[segnum];
328 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
329 /*
330 * This segment is in the process of being allocated. Wait
331 * until it's done, and look the key up again (in case the
332 * allocation failed or it was freed).
333 */
334 shmseg->shm_perm.mode |= SHMSEG_WANTED;
335 if (error =
336 tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0))
337 return error;
338 return EAGAIN;
339 }
340 if (error = ipcperm(cred, &shmseg->shm_perm, mode))
341 return error;
342 if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
343 return EINVAL;
344 if (SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL) ==
345 (IPC_CREAT | IPC_EXCL))
346 return EEXIST;
347 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
348 return 0;
349 }
350
351 static int
352 shmget_allocate_segment(p, uap, mode, retval)
353 struct proc *p;
354 struct shmget_args /* {
355 syscallarg(key_t) key;
356 syscallarg(int) size;
357 syscallarg(int) shmflag;
358 } */ *uap;
359 int mode;
360 register_t *retval;
361 {
362 int i, segnum, result, shmid, size;
363 struct ucred *cred = p->p_ucred;
364 struct shmid_ds *shmseg;
365 struct shm_handle *shm_handle;
366
367 if (SCARG(uap, size) < shminfo.shmmin ||
368 SCARG(uap, size) > shminfo.shmmax)
369 return EINVAL;
370 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
371 return ENOSPC;
372 size = (SCARG(uap, size) + CLOFSET) & ~CLOFSET;
373 if (shm_committed + btoc(size) > shminfo.shmall)
374 return ENOMEM;
375 if (shm_last_free < 0) {
376 for (i = 0; i < shminfo.shmmni; i++)
377 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
378 break;
379 if (i == shminfo.shmmni)
380 panic("shmseg free count inconsistent");
381 segnum = i;
382 } else {
383 segnum = shm_last_free;
384 shm_last_free = -1;
385 }
386 shmseg = &shmsegs[segnum];
387 /*
388 * In case we sleep in malloc(), mark the segment present but deleted
389 * so that noone else tries to create the same key.
390 */
391 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
392 shmseg->shm_perm.key = SCARG(uap, key);
393 shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
394 shm_handle = (struct shm_handle *)
395 malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
396 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
397 result = vm_mmap(sysvshm_map, &shm_handle->kva, size, VM_PROT_ALL,
398 VM_PROT_DEFAULT, MAP_ANON, (caddr_t)(long)shmid, 0);
399 if (result != KERN_SUCCESS) {
400 shmseg->shm_perm.mode = SHMSEG_FREE;
401 shm_last_free = segnum;
402 free((caddr_t)shm_handle, M_SHM);
403 /* Just in case. */
404 wakeup((caddr_t)shmseg);
405 return ENOMEM;
406 }
407 shmseg->shm_internal = shm_handle;
408 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
409 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
410 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
411 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
412 shmseg->shm_segsz = SCARG(uap, size);
413 shmseg->shm_cpid = p->p_pid;
414 shmseg->shm_lpid = shmseg->shm_nattch = 0;
415 shmseg->shm_atime = shmseg->shm_dtime = 0;
416 shmseg->shm_ctime = time.tv_sec;
417 shm_committed += btoc(size);
418 shm_nused++;
419 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
420 /*
421 * Somebody else wanted this key while we were asleep. Wake
422 * them up now.
423 */
424 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
425 wakeup((caddr_t)shmseg);
426 }
427 *retval = shmid;
428 return 0;
429 }
430
431 int
432 shmget(p, uap, retval)
433 struct proc *p;
434 struct shmget_args /* {
435 syscallarg(key_t) key;
436 syscallarg(int) size;
437 syscallarg(int) shmflag;
438 } */ *uap;
439 register_t *retval;
440 {
441 int segnum, mode, error;
442 struct shmid_ds *shmseg;
443
444 mode = SCARG(uap, shmflg) & ACCESSPERMS;
445 if (SCARG(uap, key) != IPC_PRIVATE) {
446 again:
447 segnum = shm_find_segment_by_key(SCARG(uap, key));
448 if (segnum >= 0) {
449 error = shmget_existing(p, uap, mode, segnum, retval);
450 if (error == EAGAIN)
451 goto again;
452 return error;
453 }
454 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
455 return ENOENT;
456 }
457 return shmget_allocate_segment(p, uap, mode, retval);
458 }
459
460 void
461 shmfork(p1, p2, isvfork)
462 struct proc *p1, *p2;
463 int isvfork;
464 {
465 struct shmmap_state *shmmap_s;
466 size_t size;
467 int i;
468
469 size = shminfo.shmseg * sizeof(struct shmmap_state);
470 shmmap_s = malloc(size, M_SHM, M_WAITOK);
471 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
472 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
473 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
474 if (shmmap_s->shmid != -1)
475 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
476 }
477
478 void
479 shmexit(p)
480 struct proc *p;
481 {
482 struct shmmap_state *shmmap_s;
483 struct shmid_ds *shmseg;
484 int i;
485
486 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
487 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
488 if (shmmap_s->shmid != -1)
489 shm_delete_mapping(p, shmmap_s);
490 free((caddr_t)p->p_vmspace->vm_shm, M_SHM);
491 p->p_vmspace->vm_shm = NULL;
492 }
493
494 void
495 shminit()
496 {
497 int i;
498 vm_offset_t garbage1, garbage2;
499
500 shminfo.shmmax *= NBPG;
501
502 /* actually this *should* be pageable. SHM_{LOCK,UNLOCK} */
503 sysvshm_map = kmem_suballoc(kernel_map, &garbage1, &garbage2,
504 shminfo.shmall * NBPG, TRUE);
505 for (i = 0; i < shminfo.shmmni; i++) {
506 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
507 shmsegs[i].shm_perm.seq = 0;
508 }
509 shm_last_free = 0;
510 shm_nused = 0;
511 shm_committed = 0;
512 }
513
514 #if defined(COMPAT_10) && !defined(alpha)
515 int
516 compat_10_shmsys(p, uap, retval)
517 struct proc *p;
518 struct compat_10_shmsys_args /* {
519 syscallarg(int) which;
520 syscallarg(int) a2;
521 syscallarg(int) a3;
522 syscallarg(int) a4;
523 } */ *uap;
524 register_t *retval;
525 {
526 struct shmat_args /* {
527 syscallarg(int) shmid;
528 syscallarg(void *) shmaddr;
529 syscallarg(int) shmflg;
530 } */ shmat_args;
531 struct shmctl_args /* {
532 syscallarg(int) shmid;
533 syscallarg(int) cmd;
534 syscallarg(struct shmid_ds *) buf;
535 } */ shmctl_args;
536 struct shmdt_args /* {
537 syscallarg(void *) shmaddr;
538 } */ shmdt_args;
539 struct shmget_args /* {
540 syscallarg(key_t) key;
541 syscallarg(int) size;
542 syscallarg(int) shmflg;
543 } */ shmget_args;
544
545 switch (SCARG(uap, which)) {
546 case 0: /* shmat() */
547 SCARG(&shmat_args, shmid) = SCARG(uap, a2);
548 SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a3);
549 SCARG(&shmat_args, shmflg) = SCARG(uap, a4);
550 return (shmat(p, &shmat_args, retval));
551
552 case 1: /* shmctl() */
553 SCARG(&shmctl_args, shmid) = SCARG(uap, a2);
554 SCARG(&shmctl_args, cmd) = SCARG(uap, a3);
555 SCARG(&shmctl_args, buf) = (struct shmid_ds *)SCARG(uap, a4);
556 return (shmctl(p, &shmctl_args, retval));
557
558 case 2: /* shmdt() */
559 SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a2);
560 return (shmdt(p, &shmdt_args, retval));
561
562 case 3: /* shmget() */
563 SCARG(&shmget_args, key) = SCARG(uap, a2);
564 SCARG(&shmget_args, size) = SCARG(uap, a3);
565 SCARG(&shmget_args, shmflg) = SCARG(uap, a4);
566 return (shmget(p, &shmget_args, retval));
567
568 default:
569 return (EINVAL);
570 }
571 }
572 #endif /* defined(COMPAT_10) && !defined(alpha) */
573