sysv_shm.c revision 1.51 1 /* $NetBSD: sysv_shm.c,v 1.51 1999/03/24 05:51:25 mrg 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(p->p_vmspace->vm_taddr + MAXTSIZ + MAXDSIZ);
251 }
252 shm_handle = shmseg->shm_internal;
253 uao_reference(shm_handle->shm_object);
254 rv = uvm_map(&p->p_vmspace->vm_map, &attach_va, size,
255 shm_handle->shm_object, 0,
256 UVM_MAPFLAG(prot, prot, UVM_INH_SHARE,
257 UVM_ADV_RANDOM, 0));
258 if (rv != KERN_SUCCESS) {
259 return ENOMEM;
260 }
261
262 shmmap_s->va = attach_va;
263 shmmap_s->shmid = SCARG(uap, shmid);
264 shmseg->shm_lpid = p->p_pid;
265 shmseg->shm_atime = time.tv_sec;
266 shmseg->shm_nattch++;
267 *retval = attach_va;
268 return 0;
269 }
270
271 int
272 sys_shmctl(p, v, retval)
273 struct proc *p;
274 void *v;
275 register_t *retval;
276 {
277 struct sys_shmctl_args /* {
278 syscallarg(int) shmid;
279 syscallarg(int) cmd;
280 syscallarg(struct shmid_ds *) buf;
281 } */ *uap = v;
282 int error;
283 struct ucred *cred = p->p_ucred;
284 struct shmid_ds inbuf;
285 struct shmid_ds *shmseg;
286
287 shmseg = shm_find_segment_by_shmid(SCARG(uap, shmid));
288 if (shmseg == NULL)
289 return EINVAL;
290 switch (SCARG(uap, cmd)) {
291 case IPC_STAT:
292 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_R)) != 0)
293 return error;
294 error = copyout((caddr_t)shmseg, SCARG(uap, buf),
295 sizeof(inbuf));
296 if (error)
297 return error;
298 break;
299 case IPC_SET:
300 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
301 return error;
302 error = copyin(SCARG(uap, buf), (caddr_t)&inbuf,
303 sizeof(inbuf));
304 if (error)
305 return error;
306 shmseg->shm_perm.uid = inbuf.shm_perm.uid;
307 shmseg->shm_perm.gid = inbuf.shm_perm.gid;
308 shmseg->shm_perm.mode =
309 (shmseg->shm_perm.mode & ~ACCESSPERMS) |
310 (inbuf.shm_perm.mode & ACCESSPERMS);
311 shmseg->shm_ctime = time.tv_sec;
312 break;
313 case IPC_RMID:
314 if ((error = ipcperm(cred, &shmseg->shm_perm, IPC_M)) != 0)
315 return error;
316 shmseg->shm_perm.key = IPC_PRIVATE;
317 shmseg->shm_perm.mode |= SHMSEG_REMOVED;
318 if (shmseg->shm_nattch <= 0) {
319 shm_deallocate_segment(shmseg);
320 shm_last_free = IPCID_TO_IX(SCARG(uap, shmid));
321 }
322 break;
323 case SHM_LOCK:
324 case SHM_UNLOCK:
325 default:
326 return EINVAL;
327 }
328 return 0;
329 }
330
331 static int
332 shmget_existing(p, uap, mode, segnum, retval)
333 struct proc *p;
334 struct sys_shmget_args /* {
335 syscallarg(key_t) key;
336 syscallarg(size_t) size;
337 syscallarg(int) shmflg;
338 } */ *uap;
339 int mode;
340 int segnum;
341 register_t *retval;
342 {
343 struct shmid_ds *shmseg;
344 struct ucred *cred = p->p_ucred;
345 int error;
346
347 shmseg = &shmsegs[segnum];
348 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
349 /*
350 * This segment is in the process of being allocated. Wait
351 * until it's done, and look the key up again (in case the
352 * allocation failed or it was freed).
353 */
354 shmseg->shm_perm.mode |= SHMSEG_WANTED;
355 error = tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0);
356 if (error)
357 return error;
358 return EAGAIN;
359 }
360 if ((error = ipcperm(cred, &shmseg->shm_perm, mode)) != 0)
361 return error;
362 if (SCARG(uap, size) && SCARG(uap, size) > shmseg->shm_segsz)
363 return EINVAL;
364 if ((SCARG(uap, shmflg) & (IPC_CREAT | IPC_EXCL)) ==
365 (IPC_CREAT | IPC_EXCL))
366 return EEXIST;
367 *retval = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
368 return 0;
369 }
370
371 static int
372 shmget_allocate_segment(p, uap, mode, retval)
373 struct proc *p;
374 struct sys_shmget_args /* {
375 syscallarg(key_t) key;
376 syscallarg(size_t) size;
377 syscallarg(int) shmflg;
378 } */ *uap;
379 int mode;
380 register_t *retval;
381 {
382 int i, segnum, shmid, size;
383 struct ucred *cred = p->p_ucred;
384 struct shmid_ds *shmseg;
385 struct shm_handle *shm_handle;
386 int error = 0;
387
388 if (SCARG(uap, size) < shminfo.shmmin ||
389 SCARG(uap, size) > shminfo.shmmax)
390 return EINVAL;
391 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
392 return ENOSPC;
393 size = (SCARG(uap, size) + CLOFSET) & ~CLOFSET;
394 if (shm_committed + btoc(size) > shminfo.shmall)
395 return ENOMEM;
396 if (shm_last_free < 0) {
397 for (i = 0; i < shminfo.shmmni; i++)
398 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
399 break;
400 if (i == shminfo.shmmni)
401 panic("shmseg free count inconsistent");
402 segnum = i;
403 } else {
404 segnum = shm_last_free;
405 shm_last_free = -1;
406 }
407 shmseg = &shmsegs[segnum];
408 /*
409 * In case we sleep in malloc(), mark the segment present but deleted
410 * so that noone else tries to create the same key.
411 */
412 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
413 shmseg->shm_perm.key = SCARG(uap, key);
414 shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
415 shm_handle = (struct shm_handle *)
416 malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
417 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
418
419 shm_handle->shm_object = uao_create(size, 0);
420
421 shmseg->shm_internal = shm_handle;
422 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
423 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
424 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
425 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
426 shmseg->shm_segsz = SCARG(uap, size);
427 shmseg->shm_cpid = p->p_pid;
428 shmseg->shm_lpid = shmseg->shm_nattch = 0;
429 shmseg->shm_atime = shmseg->shm_dtime = 0;
430 shmseg->shm_ctime = time.tv_sec;
431 shm_committed += btoc(size);
432 shm_nused++;
433
434 *retval = shmid;
435 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
436 /*
437 * Somebody else wanted this key while we were asleep. Wake
438 * them up now.
439 */
440 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
441 wakeup((caddr_t)shmseg);
442 }
443 return error;
444 }
445
446 int
447 sys_shmget(p, v, retval)
448 struct proc *p;
449 void *v;
450 register_t *retval;
451 {
452 struct sys_shmget_args /* {
453 syscallarg(key_t) key;
454 syscallarg(int) size;
455 syscallarg(int) shmflg;
456 } */ *uap = v;
457 int segnum, mode, error;
458
459 mode = SCARG(uap, shmflg) & ACCESSPERMS;
460 if (SCARG(uap, key) != IPC_PRIVATE) {
461 again:
462 segnum = shm_find_segment_by_key(SCARG(uap, key));
463 if (segnum >= 0) {
464 error = shmget_existing(p, uap, mode, segnum, retval);
465 if (error == EAGAIN)
466 goto again;
467 return error;
468 }
469 if ((SCARG(uap, shmflg) & IPC_CREAT) == 0)
470 return ENOENT;
471 }
472 return shmget_allocate_segment(p, uap, mode, retval);
473 }
474
475 void
476 shmfork(vm1, vm2)
477 struct vmspace *vm1, *vm2;
478 {
479 struct shmmap_state *shmmap_s;
480 size_t size;
481 int i;
482
483 if (vm1->vm_shm == NULL) {
484 vm2->vm_shm = NULL;
485 return;
486 }
487
488 size = shminfo.shmseg * sizeof(struct shmmap_state);
489 shmmap_s = malloc(size, M_SHM, M_WAITOK);
490 memcpy(shmmap_s, vm1->vm_shm, size);
491 vm2->vm_shm = (caddr_t)shmmap_s;
492 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
493 if (shmmap_s->shmid != -1)
494 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
495 }
496
497 void
498 shmexit(vm)
499 struct vmspace *vm;
500 {
501 struct shmmap_state *shmmap_s;
502 int i;
503
504 shmmap_s = (struct shmmap_state *)vm->vm_shm;
505 if (shmmap_s == NULL)
506 return;
507 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
508 if (shmmap_s->shmid != -1)
509 shm_delete_mapping(vm, shmmap_s);
510 free(vm->vm_shm, M_SHM);
511 vm->vm_shm = NULL;
512 }
513
514 void
515 shminit()
516 {
517 int i;
518
519 shminfo.shmmax *= NBPG;
520
521 for (i = 0; i < shminfo.shmmni; i++) {
522 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
523 shmsegs[i].shm_perm.seq = 0;
524 }
525 shm_last_free = 0;
526 shm_nused = 0;
527 shm_committed = 0;
528 }
529