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