sysv_shm.c revision 1.18 1 /*
2 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Adam Glass and Charles
15 * Hannum.
16 * 4. The names of the authors may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/shm.h>
35 #include <sys/proc.h>
36 #include <sys/uio.h>
37 #include <sys/time.h>
38 #include <sys/malloc.h>
39 #include <sys/mman.h>
40 #include <sys/systm.h>
41 #include <sys/stat.h>
42
43 #include <vm/vm.h>
44 #include <vm/vm_map.h>
45 #include <vm/vm_map.h>
46 #include <vm/vm_kern.h>
47
48 /*
49 * Provides the following externally accessible functions:
50 *
51 * shminit(void); initialization
52 * shmexit(struct proc *) cleanup
53 * shmfork(struct proc *, struct proc *, int) fork handling
54 * shmsys(arg1, arg2, arg3, arg4); shm{at,ctl,dt,get}(arg2, arg3, arg4)
55 *
56 * Structures:
57 * shmsegs (an array of 'struct shmid_ds')
58 * per proc array of 'struct shmmap_state'
59 */
60
61 int shmat(), shmctl(), shmdt(), shmget();
62 int (*shmcalls[])() = { shmat, shmctl, shmdt, shmget };
63
64 #define SHMSEG_FREE 0x0200
65 #define SHMSEG_REMOVED 0x0400
66 #define SHMSEG_ALLOCATED 0x0800
67 #define SHMSEG_WANTED 0x1000
68
69 vm_map_t sysvshm_map;
70 int shm_last_free, shm_nused, shm_committed;
71
72 struct shm_handle {
73 vm_offset_t kva;
74 };
75
76 struct shmmap_state {
77 vm_offset_t va;
78 int shmid;
79 };
80
81 static void shm_deallocate_segment __P((struct shmid_ds *));
82 static int shm_find_segment_by_key __P((key_t));
83 static struct shmid_ds *shm_find_segment_by_shmid __P((int));
84 static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
85
86 static int
87 shm_find_segment_by_key(key)
88 key_t key;
89 {
90 int i;
91
92 for (i = 0; i < shminfo.shmmni; i++)
93 if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
94 shmsegs[i].shm_perm.key == key)
95 return i;
96 return -1;
97 }
98
99 static struct shmid_ds *
100 shm_find_segment_by_shmid(shmid)
101 int shmid;
102 {
103 int segnum;
104 struct shmid_ds *shmseg;
105
106 segnum = IPCID_TO_IX(shmid);
107 if (segnum < 0 || segnum >= shminfo.shmmni)
108 return NULL;
109 shmseg = &shmsegs[segnum];
110 if ((shmseg->shm_perm.mode & (SHMSEG_ALLOCATED | SHMSEG_REMOVED))
111 != SHMSEG_ALLOCATED ||
112 shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
113 return NULL;
114 return shmseg;
115 }
116
117 static vm_offset_t
118 shm_find_space(p, size)
119 struct proc *p;
120 size_t size;
121 {
122 vm_offset_t low_end, range, current;
123 int result;
124
125 low_end = (vm_offset_t)p->p_vmspace->vm_daddr +
126 (p->p_vmspace->vm_dsize << PGSHIFT);
127 range = (USRSTACK - low_end);
128
129 /* XXXX totally bogus */
130 /* current = range *3/4 + low_end */
131 current = ((range&1)<<1 + range)>>2 + range>>1 + low_end;
132 #if 0
133 result = vm_map_find(&p->p_vmspace->vm_map, NULL, 0, ¤t, size,
134 TRUE);
135 if (result)
136 return NULL;
137 #endif
138 return current;
139 }
140
141 static void
142 shm_deallocate_segment(shmseg)
143 struct shmid_ds *shmseg;
144 {
145 struct shm_handle *shm_handle;
146 size_t size;
147
148 shm_handle = shmseg->shm_internal;
149 size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
150 vm_deallocate(sysvshm_map, shm_handle->kva, size);
151 free((caddr_t)shm_handle, M_SHM);
152 shmseg->shm_internal = NULL;
153 shm_committed -= btoc(size);
154 shmseg->shm_perm.mode = SHMSEG_FREE;
155 }
156
157 static int
158 shm_delete_mapping(p, shmmap_s)
159 struct proc *p;
160 struct shmmap_state *shmmap_s;
161 {
162 struct shmid_ds *shmseg;
163 int segnum, result;
164 size_t size;
165
166 segnum = IPCID_TO_IX(shmmap_s->shmid);
167 shmseg = &shmsegs[segnum];
168 size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
169 result = vm_deallocate(&p->p_vmspace->vm_map, shmmap_s->va, size);
170 if (result != KERN_SUCCESS)
171 return EINVAL;
172 shmmap_s->shmid = -1;
173 shmseg->shm_dtime = time.tv_sec;
174 if ((--shmseg->shm_nattch <= 0) &&
175 (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
176 shm_deallocate_segment(shmseg);
177 shm_last_free = segnum;
178 }
179 return 0;
180 }
181
182 struct shmdt_args {
183 void *shmaddr;
184 };
185 int
186 shmdt(p, uap, retval)
187 struct proc *p;
188 struct shmdt_args *uap;
189 int *retval;
190 {
191 struct shmmap_state *shmmap_s;
192 int i;
193
194 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
195 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
196 if (shmmap_s->shmid != -1 &&
197 shmmap_s->va == (vm_offset_t)uap->shmaddr)
198 break;
199 if (i == shminfo.shmseg)
200 return EINVAL;
201 return shm_delete_mapping(p, shmmap_s);
202 }
203
204 struct shmat_args {
205 int shmid;
206 void *shmaddr;
207 int shmflg;
208 };
209 int
210 shmat(p, uap, retval)
211 struct proc *p;
212 struct shmat_args *uap;
213 int *retval;
214 {
215 int error, i, flags;
216 struct ucred *cred = p->p_ucred;
217 struct shmid_ds *shmseg;
218 struct shmmap_state *shmmap_s = NULL;
219 vm_offset_t attach_va;
220 vm_prot_t prot;
221 vm_size_t size;
222
223 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
224 if (shmmap_s == NULL) {
225 size = shminfo.shmseg * sizeof(struct shmmap_state);
226 shmmap_s = malloc(size, M_SHM, M_WAITOK);
227 bzero((caddr_t)shmmap_s, size);
228 for (i = 0; i < shminfo.shmseg; i++)
229 shmmap_s[i].shmid = -1;
230 p->p_vmspace->vm_shm = (caddr_t)shmmap_s;
231 }
232 shmseg = shm_find_segment_by_shmid(uap->shmid);
233 if (shmseg == NULL)
234 return EINVAL;
235 if (error = ipcperm(cred, &shmseg->shm_perm,
236 (uap->shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W))
237 return error;
238 for (i = 0; i < shminfo.shmseg; i++) {
239 if (shmmap_s->shmid == -1)
240 break;
241 shmmap_s++;
242 }
243 if (i >= shminfo.shmseg)
244 return EMFILE;
245 size = (shmseg->shm_segsz + CLOFSET) & ~CLOFSET;
246 prot = VM_PROT_READ;
247 if ((uap->shmflg & SHM_RDONLY) == 0)
248 prot |= VM_PROT_WRITE;
249 flags = MAP_ANON | MAP_SHARED;
250 if (uap->shmaddr) {
251 flags |= MAP_FIXED;
252 if (uap->shmflg & SHM_RND)
253 attach_va = (vm_offset_t)uap->shmaddr & ~(SHMLBA-1);
254 else if (((vm_offset_t)uap->shmaddr & (SHMLBA-1)) == 0)
255 attach_va = (vm_offset_t)uap->shmaddr;
256 else
257 return EINVAL;
258 } else {
259 attach_va = shm_find_space(p, shmseg->shm_segsz);
260 if (attach_va == NULL)
261 return ENOMEM;
262 }
263 error = vm_mmap(&p->p_vmspace->vm_map, &attach_va, size, prot,
264 VM_PROT_DEFAULT, flags, uap->shmid, 0);
265 if (error)
266 return error;
267 shmmap_s->va = attach_va;
268 shmmap_s->shmid = uap->shmid;
269 shmseg->shm_lpid = p->p_pid;
270 shmseg->shm_atime = time.tv_sec;
271 shmseg->shm_nattch++;
272 *retval = attach_va;
273 return 0;
274 }
275
276 struct shmctl_args {
277 int shmid;
278 int cmd;
279 struct shmat_ds *ubuf;
280 };
281 int
282 shmctl(p, uap, retval)
283 struct proc *p;
284 struct shmctl_args *uap;
285 int *retval;
286 {
287 int error, segnum;
288 struct ucred *cred = p->p_ucred;
289 struct shmid_ds inbuf;
290 struct shmid_ds *shmseg;
291
292 shmseg = shm_find_segment_by_shmid(uap->shmid);
293 if (shmseg == NULL)
294 return EINVAL;
295 switch (uap->cmd) {
296 case IPC_STAT:
297 if (error = ipcperm(cred, &shmseg->shm_perm, IPC_R))
298 return error;
299 if (error = copyout((caddr_t)shmseg, uap->ubuf, sizeof(inbuf)))
300 return error;
301 break;
302 case IPC_SET:
303 if (error = ipcperm(cred, &shmseg->shm_perm, IPC_M))
304 return error;
305 if (error = copyin(uap->ubuf, (caddr_t)&inbuf, sizeof(inbuf)))
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))
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(uap->shmid);
322 }
323 break;
324 #if 0
325 case SHM_LOCK:
326 case SHM_UNLOCK:
327 #endif
328 default:
329 return EINVAL;
330 }
331 return 0;
332 }
333
334 struct shmget_args {
335 key_t key;
336 size_t size;
337 int shmflg;
338 };
339 static int
340 shmget_existing(p, uap, mode, segnum, retval)
341 struct proc *p;
342 struct shmget_args *uap;
343 int mode;
344 int segnum;
345 int *retval;
346 {
347 struct shmid_ds *shmseg;
348 struct ucred *cred = p->p_ucred;
349 int error;
350
351 shmseg = &shmsegs[segnum];
352 if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
353 /*
354 * This segment is in the process of being allocated. Wait
355 * until it's done, and look the key up again (in case the
356 * allocation failed or it was freed).
357 */
358 shmseg->shm_perm.mode |= SHMSEG_WANTED;
359 if (error =
360 tsleep((caddr_t)shmseg, PLOCK | PCATCH, "shmget", 0))
361 return error;
362 return EAGAIN;
363 }
364 if (error = ipcperm(cred, &shmseg->shm_perm, mode))
365 return error;
366 if (uap->size && uap->size > shmseg->shm_segsz)
367 return EINVAL;
368 if (uap->shmflg & (IPC_CREAT | IPC_EXCL) == (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 shmget_args *uap;
378 int mode;
379 int *retval;
380 {
381 int i, segnum, result, shmid, size;
382 struct ucred *cred = p->p_ucred;
383 struct shmid_ds *shmseg;
384 struct shm_handle *shm_handle;
385
386 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
387 return EINVAL;
388 if (shm_nused >= shminfo.shmmni) /* any shmids left? */
389 return ENOSPC;
390 size = (uap->size + CLOFSET) & ~CLOFSET;
391 if (shm_committed + btoc(size) > shminfo.shmall)
392 return ENOMEM;
393 if (shm_last_free < 0) {
394 for (i = 0; i < shminfo.shmmni; i++)
395 if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
396 break;
397 if (i == shminfo.shmmni)
398 panic("shmseg free count inconsistent");
399 segnum = i;
400 } else {
401 segnum = shm_last_free;
402 shm_last_free = -1;
403 }
404 shmseg = &shmsegs[segnum];
405 /*
406 * In case we sleep in malloc(), mark the segment present but deleted
407 * so that noone else tries to create the same key.
408 */
409 shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
410 shmseg->shm_perm.key = uap->key;
411 shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
412 shm_handle = (struct shm_handle *)
413 malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
414 shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
415 result = vm_mmap(sysvshm_map, &shm_handle->kva, size, VM_PROT_ALL,
416 VM_PROT_DEFAULT, MAP_ANON, shmid, 0);
417 if (result != KERN_SUCCESS) {
418 shmseg->shm_perm.mode = SHMSEG_FREE;
419 shm_last_free = segnum;
420 free((caddr_t)shm_handle, M_SHM);
421 /* Just in case. */
422 wakeup((caddr_t)shmseg);
423 return ENOMEM;
424 }
425 shmseg->shm_internal = shm_handle;
426 shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
427 shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
428 shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
429 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
430 shmseg->shm_segsz = uap->size;
431 shmseg->shm_cpid = p->p_pid;
432 shmseg->shm_lpid = shmseg->shm_nattch = 0;
433 shmseg->shm_atime = shmseg->shm_dtime = 0;
434 shmseg->shm_ctime = time.tv_sec;
435 shm_committed += btoc(size);
436 shm_nused++;
437 if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
438 /*
439 * Somebody else wanted this key while we were asleep. Wake
440 * them up now.
441 */
442 shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
443 wakeup((caddr_t)shmseg);
444 }
445 *retval = shmid;
446 return 0;
447 }
448
449 int
450 shmget(p, uap, retval)
451 struct proc *p;
452 struct shmget_args *uap;
453 int *retval;
454 {
455 int segnum, mode, error;
456 struct shmid_ds *shmseg;
457
458 mode = uap->shmflg & ACCESSPERMS;
459 if (uap->key != IPC_PRIVATE) {
460 again:
461 segnum = shm_find_segment_by_key(uap->key);
462 if (segnum >= 0) {
463 error = shmget_existing(p, uap, mode, segnum, retval);
464 if (error == EAGAIN)
465 goto again;
466 return error;
467 }
468 if ((uap->shmflg & IPC_CREAT) == 0)
469 return ENOENT;
470 }
471 return shmget_allocate_segment(p, uap, mode, retval);
472 }
473
474 struct shmsys_args {
475 u_int which;
476 };
477 int
478 shmsys(p, uap, retval)
479 struct proc *p;
480 struct shmsys_args *uap;
481 int *retval;
482 {
483
484 if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
485 return EINVAL;
486 return ((*shmcalls[uap->which])(p, &uap[1], retval));
487 }
488
489 void
490 shmfork(p1, p2, isvfork)
491 struct proc *p1, *p2;
492 int isvfork;
493 {
494 struct shmmap_state *shmmap_s;
495 size_t size;
496 int i;
497
498 size = shminfo.shmseg * sizeof(struct shmmap_state);
499 shmmap_s = malloc(size, M_SHM, M_WAITOK);
500 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
501 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
502 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
503 if (shmmap_s->shmid != -1)
504 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
505 }
506
507 void
508 shmexit(p)
509 struct proc *p;
510 {
511 struct shmmap_state *shmmap_s;
512 struct shmid_ds *shmseg;
513 int i;
514
515 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
516 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
517 if (shmmap_s->shmid != -1)
518 shm_delete_mapping(p, shmmap_s);
519 free((caddr_t)p->p_vmspace->vm_shm, M_SHM);
520 p->p_vmspace->vm_shm = NULL;
521 }
522
523 void
524 shminit()
525 {
526 int i;
527 vm_offset_t garbage1, garbage2;
528
529 /* actually this *should* be pageable. SHM_{LOCK,UNLOCK} */
530 sysvshm_map = kmem_suballoc(kernel_map, &garbage1, &garbage2,
531 shminfo.shmall * NBPG, FALSE);
532 for (i = 0; i < shminfo.shmmni; i++) {
533 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
534 shmsegs[i].shm_perm.seq = 0;
535 }
536 shm_last_free = 0;
537 shm_nused = 0;
538 shm_committed = 0;
539 }
540