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