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