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