sysv_shm.c revision 1.39 1 /* $NetBSD: sysv_shm.c,v 1.39 1997/10/07 10:02:03 drochner 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 struct shmid_ds *shm_find_segment_by_shmid __P((int));
54 void shmexit __P((struct proc *));
55
56 /*
57 * Provides the following externally accessible functions:
58 *
59 * shminit(void); initialization
60 * shmexit(struct proc *) cleanup
61 * shmfork(struct proc *, struct proc *) fork handling
62 * shmsys(arg1, arg2, arg3, arg4); shm{at,ctl,dt,get}(arg2, arg3, arg4)
63 *
64 * Structures:
65 * shmsegs (an array of 'struct shmid_ds')
66 * per proc array of 'struct shmmap_state'
67 */
68
69 #define SHMSEG_FREE 0x0200
70 #define SHMSEG_REMOVED 0x0400
71 #define SHMSEG_ALLOCATED 0x0800
72 #define SHMSEG_WANTED 0x1000
73
74 int shm_last_free, shm_nused, shm_committed;
75
76 struct shm_handle {
77 vm_object_t shm_object;
78 };
79
80 struct shmmap_state {
81 vm_offset_t va;
82 int shmid;
83 };
84
85 static int shm_find_segment_by_key __P((key_t));
86 static void shm_deallocate_segment __P((struct shmid_ds *));
87 static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
88 static int shmget_existing __P((struct proc *, struct sys_shmget_args *,
89 int, int, register_t *));
90 static int shmget_allocate_segment __P((struct proc *, struct sys_shmget_args *,
91 int, register_t *));
92
93 static int
94 shm_find_segment_by_key(key)
95 key_t key;
96 {
97 int i;
98
99 for (i = 0; i < shminfo.shmmni; i++)
100 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
101 shmsegs[i].shm_perm.key == key)
102 return i;
103 return -1;
104 }
105
106 struct shmid_ds *
107 shm_find_segment_by_shmid(shmid)
108 int shmid;
109 {
110 int segnum;
111 struct shmid_ds *shmseg;
112
113 segnum = IPCID_TO_IX(shmid);
114 if (segnum < 0 || segnum >= shminfo.shmmni)
115 return NULL;
116 shmseg = &shmsegs[segnum];
117 if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
118 != SHMSEG_ALLOCATED ||
119 shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
120 return NULL;
121 return shmseg;
122 }
123
124 static void
125 shm_deallocate_segment(shmseg)
126 struct shmid_ds *shmseg;
127 {
128 struct shm_handle *shm_handle;
129 size_t size;
130
131 shm_handle = shmseg->shm_internal;
132 size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
133 vm_object_deallocate(shm_handle->shm_object);
134 free((caddr_t)shm_handle, M_SHM);
135 shmseg->shm_internal = NULL;
136 shm_committed -= btoc(size);
137 shmseg->shm_perm.mode = SHMSEG_FREE;
138 shm_nused--;
139 }
140
141 static int
142 shm_delete_mapping(p, shmmap_s)
143 struct proc *p;
144 struct shmmap_state *shmmap_s;
145 {
146 struct shmid_ds *shmseg;
147 int segnum, result;
148 size_t size;
149
150 segnum = IPCID_TO_IX(shmmap_s->shmid);
151 shmseg = &shmsegs[segnum];
152 size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
153 result = vm_map_remove(&p->p_vmspace->vm_map, shmmap_s->va, shmmap_s->va + size);
154 if (result != KERN_SUCCESS)
155 return EINVAL;
156 shmmap_s->shmid = -1;
157 shmseg->shm_dtime = time.tv_sec;
158 if ((--shmseg->shm_nattch <= 0) &&
159 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
160 shm_deallocate_segment(shmseg);
161 shm_last_free = segnum;
162 }
163 return 0;
164 }
165
166 int
167 sys_shmdt(p, v, retval)
168 struct proc *p;
169 void *v;
170 register_t *retval;
171 {
172 struct sys_shmdt_args /* {
173 syscallarg(void *) shmaddr;
174 } */ *uap = v;
175 struct shmmap_state *shmmap_s;
176 int i;
177
178 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
179 if (shmmap_s == NULL)
180 return EINVAL;
181
182 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
183 if (shmmap_s->shmid != -1 &&
184 shmmap_s->va == (vm_offset_t)SCARG(uap, shmaddr))
185 break;
186 if (i == shminfo.shmseg)
187 return EINVAL;
188 return shm_delete_mapping(p, shmmap_s);
189 }
190
191 int
192 sys_shmat(p, v, retval)
193 struct proc *p;
194 void *v;
195 register_t *retval;
196 {
197 struct sys_shmat_args /* {
198 syscallarg(int) shmid;
199 syscallarg(void *) shmaddr;
200 syscallarg(int) shmflg;
201 } */ *uap = v;
202 int error, i, flags;
203 struct ucred *cred = p->p_ucred;
204 struct shmid_ds *shmseg;
205 struct shmmap_state *shmmap_s = NULL;
206 struct shm_handle *shm_handle;
207 vm_offset_t attach_va;
208 vm_prot_t prot;
209 vm_size_t size;
210 int rv;
211
212 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
213 if (shmmap_s == NULL) {
214 size = shminfo.shmseg * sizeof(struct shmmap_state);
215 shmmap_s = malloc(size, M_SHM, M_WAITOK);
216 for (i = 0; i < shminfo.shmseg; i++)
217 shmmap_s[i].shmid = -1;
218 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
219 }
220 shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
221 if (shmseg == NULL)
222 return EINVAL;
223 error = ipcperm(cred, &shmseg->shm_perm,
224 (SCARG(uap, shmflg) & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
225 if (error)
226 return error;
227 for (i = 0; i < shminfo.shmseg; i++) {
228 if (shmmap_s->shmid == -1)
229 break;
230 shmmap_s++;
231 }
232 if (i >= shminfo.shmseg)
233 return EMFILE;
234 size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
235 prot = VM_PROT_READ;
236 if ((SCARG(uap, shmflg) & SHM_RDONLY) == 0)
237 prot |= VM_PROT_WRITE;
238 flags = MAP_ANON | MAP_SHARED;
239 if (SCARG(uap, shmaddr)) {
240 flags |= MAP_FIXED;
241 if (SCARG(uap, shmflg) & SHM_RND)
242 attach_va =
243 (vm_offset_t)SCARG(uap, shmaddr) & ~(SHMLBA-1);
244 else if (((vm_offset_t)SCARG(uap, shmaddr) & (SHMLBA-1)) == 0)
245 attach_va = (vm_offset_t)SCARG(uap, shmaddr);
246 else
247 return EINVAL;
248 } else {
249 /* This is just a hint to vm_mmap() about where to put it. */
250 attach_va =
251 round_page(p->p_vmspace->vm_taddr + MAXTSIZ + MAXDSIZ);
252 }
253 shm_handle = shmseg->shm_internal;
254 vm_object_reference(shm_handle->shm_object);
255 rv = vm_map_find(&p->p_vmspace->vm_map, shm_handle->shm_object,
256 0, &attach_va, size, (flags & MAP_FIXED)?0:1);
257 if (rv != KERN_SUCCESS) {
258 return ENOMEM;
259 }
260 vm_map_protect(&p->p_vmspace->vm_map, attach_va, attach_va + size,
261 prot, 0);
262 vm_map_inherit(&p->p_vmspace->vm_map,
263 attach_va, attach_va + size, VM_INHERIT_SHARE);
264
265 shmmap_s->va = attach_va;
266 shmmap_s->shmid = SCARG(uap, shmid);
267 shmseg->shm_lpid = p->p_pid;
268 shmseg->shm_atime = time.tv_sec;
269 shmseg->shm_nattch++;
270 *retval = attach_va;
271 return 0;
272 }
273
274 int
275 sys_shmctl(p, v, retval)
276 struct proc *p;
277 void *v;
278 register_t *retval;
279 {
280 struct sys_shmctl_args /* {
281 syscallarg(int) shmid;
282 syscallarg(int) cmd;
283 syscallarg(struct shmid_ds *) buf;
284 } */ *uap = v;
285 int error;
286 struct ucred *cred = p->p_ucred;
287 struct shmid_ds inbuf;
288 struct shmid_ds *shmseg;
289
290 shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
291 if (shmseg == NULL)
292 return EINVAL;
293 switch (SCARG(uap, cmd)) {
294 case IPC_STAT:
295 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
296 return error;
297 error = copyout((caddr_t)shmseg, SCARG(uap, buf),
298 sizeof(inbuf));
299 if (error)
300 return error;
301 break;
302 case IPC_SET:
303 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
304 return error;
305 error = copyin(SCARG(uap, buf), (caddr_t)&inbuf,
306 sizeof(inbuf));
307 if (error)
308 return error;
309 shmseg->shm_perm.uid = inbuf.shm_perm.uid;
310 shmseg->shm_perm.gid = inbuf.shm_perm.gid;
311 shmseg->shm_perm.mode =
312 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
313 (inbuf.shm_perm.mode & ACCESSPERMS);
314 shmseg->shm_ctime = time.tv_sec;
315 break;
316 case IPC_RMID:
317 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
318 return error;
319 shmseg->shm_perm.key = IPC_PRIVATE;
320 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
321 if (shmseg->shm_nattch <= 0) {
322 shm_deallocate_segment(shmseg);
323 shm_last_free = IPCID_TO_IX(SCARG(uap, shmid));
324 }
325 break;
326 case SHM_LOCK:
327 case SHM_UNLOCK:
328 default:
329 return EINVAL;
330 }
331 return 0;
332 }
333
334 static int
335 shmget_existing(p, uap, mode, segnum, retval)
336 struct proc *p;
337 struct sys_shmget_args /* {
338 syscallarg(key_t) key;
339 syscallarg(int) size;
340 syscallarg(int) shmflg;
341 } */ *uap;
342 int mode;
343 int segnum;
344 register_t *retval;
345 {
346 struct shmid_ds *shmseg;
347 struct ucred *cred = p->p_ucred;
348 int error;
349
350 shmseg = &shmsegs[segnum];
351 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
352 /*
353 * This segment is in the process of being allocated. Wait
354 * until it's done, and look the key up again (in case the
355 * allocation failed or it was freed).
356 */
357 shmseg->shm_perm.mode |= SHMSEG_WANTED;
358 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
359 if (error)
360 return error;
361 return EAGAIN;
362 }
363 if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
364 return error;
365 if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
366 return EINVAL;
367 if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
368 (IPC_CREAT | IPC_EXCL))
369 return EEXIST;
370 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
371 return 0;
372 }
373
374 static int
375 shmget_allocate_segment(p, uap, mode, retval)
376 struct proc *p;
377 struct sys_shmget_args /* {
378 syscallarg(key_t) key;
379 syscallarg(int) size;
380 syscallarg(int) shmflg;
381 } */ *uap;
382 int mode;
383 register_t *retval;
384 {
385 int i, segnum, shmid, size;
386 struct ucred *cred = p->p_ucred;
387 struct shmid_ds *shmseg;
388 struct shm_handle *shm_handle;
389 vm_pager_t pager;
390
391 if (SCARG(uap, size) < shminfo.shmmin ||
392 SCARG(uap, size) > shminfo.shmmax)
393 return EINVAL;
394 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
395 return ENOSPC;
396 size = (SCARG(uap, size) + CLOFSET) & ~CLOFSET;
397 if (shm_committed + btoc(size) > shminfo.shmall)
398 return ENOMEM;
399 if (shm_last_free < 0) {
400 for (i = 0; i < shminfo.shmmni; i++)
401 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
402 break;
403 if (i == shminfo.shmmni)
404 panic("shmseg free count inconsistent");
405 segnum = i;
406 } else {
407 segnum = shm_last_free;
408 shm_last_free = -1;
409 }
410 shmseg = &shmsegs[segnum];
411 /*
412 * In case we sleep in malloc(), mark the segment present but deleted
413 * so that noone else tries to create the same key.
414 */
415 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
416 shmseg->shm_perm.key = SCARG(uap, key);
417 shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
418 shm_handle = (struct shm_handle *)
419 malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
420 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
421
422 shm_handle->shm_object = vm_object_allocate(size);
423 if (shm_handle->shm_object == NULL) {
424 free(shm_handle, M_SHM);
425 shmseg->shm_perm.mode = SHMSEG_FREE;
426 return ENOMEM;
427 }
428 /*
429 * We make sure that we have allocated a pager before we need
430 * to.
431 */
432 pager = vm_pager_allocate(PG_DFLT, 0, size, VM_PROT_DEFAULT, 0);
433 if (pager == NULL) {
434 vm_object_deallocate(shm_handle->shm_object);
435 free(shm_handle, M_SHM);
436 shmseg->shm_perm.mode = SHMSEG_FREE;
437 return ENOMEM;
438 }
439 vm_object_setpager(shm_handle->shm_object, pager, 0, 0);
440 shmseg->shm_internal = shm_handle;
441 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
442 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
443 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
444 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
445 shmseg->shm_segsz = SCARG(uap, size);
446 shmseg->shm_cpid = p->p_pid;
447 shmseg->shm_lpid = shmseg->shm_nattch = 0;
448 shmseg->shm_atime = shmseg->shm_dtime = 0;
449 shmseg->shm_ctime = time.tv_sec;
450 shm_committed += btoc(size);
451 shm_nused++;
452 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
453 /*
454 * Somebody else wanted this key while we were asleep. Wake
455 * them up now.
456 */
457 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
458 wakeup((caddr_t)shmseg);
459 }
460 *retval = shmid;
461 return 0;
462 }
463
464 int
465 sys_shmget(p, v, retval)
466 struct proc *p;
467 void *v;
468 register_t *retval;
469 {
470 struct sys_shmget_args /* {
471 syscallarg(key_t) key;
472 syscallarg(int) size;
473 syscallarg(int) shmflg;
474 } */ *uap = v;
475 int segnum, mode, error;
476
477 mode = SCARG(uap, shmflg) & ACCESSPERMS;
478 if (SCARG(uap, key) != IPC_PRIVATE) {
479 again:
480 segnum = shm_find_segment_by_key(SCARG(uap, key));
481 if (segnum >= 0) {
482 error = shmget_existing(p, uap, mode, segnum, retval);
483 if (error == EAGAIN)
484 goto again;
485 return error;
486 }
487 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
488 return ENOENT;
489 }
490 return shmget_allocate_segment(p, uap, mode, retval);
491 }
492
493 void
494 shmfork(p1, p2)
495 struct proc *p1, *p2;
496 {
497 struct shmmap_state *shmmap_s;
498 size_t size;
499 int i;
500
501 if (p1->p_vmspace->vm_shm == NULL) {
502 p2->p_vmspace->vm_shm = NULL;
503 return;
504 }
505
506 size = shminfo.shmseg * sizeof(struct shmmap_state);
507 shmmap_s = malloc(size, M_SHM, M_WAITOK);
508 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
509 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
510 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
511 if (shmmap_s->shmid != -1)
512 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
513 }
514
515 void
516 shmexit(p)
517 struct proc *p;
518 {
519 struct shmmap_state *shmmap_s;
520 int i;
521
522 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
523 if (shmmap_s == NULL)
524 return;
525 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
526 if (shmmap_s->shmid != -1)
527 shm_delete_mapping(p, shmmap_s);
528 free((caddr_t)p->p_vmspace->vm_shm, M_SHM);
529 p->p_vmspace->vm_shm = NULL;
530 }
531
532 void
533 shminit()
534 {
535 int i;
536
537 shminfo.shmmax *= NBPG;
538
539 for (i = 0; i < shminfo.shmmni; i++) {
540 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
541 shmsegs[i].shm_perm.seq = 0;
542 }
543 shm_last_free = 0;
544 shm_nused = 0;
545 shm_committed = 0;
546 }
547