xref: /freebsd/sys/kern/sysv_shm.c (revision 7773002178c8dbc52b44e4d705f07706409af8e4)
1 /*	$NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $	*/
2 /*
3  * Copyright (c) 1994 Adam Glass and Charles Hannum.  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. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Adam Glass and Charles
16  *	Hannum.
17  * 4. The names of the authors may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_compat.h"
36 #include "opt_sysvipc.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/sysctl.h>
43 #include <sys/shm.h>
44 #include <sys/proc.h>
45 #include <sys/malloc.h>
46 #include <sys/mman.h>
47 #include <sys/mutex.h>
48 #include <sys/stat.h>
49 #include <sys/syscall.h>
50 #include <sys/syscallsubr.h>
51 #include <sys/sysent.h>
52 #include <sys/sysproto.h>
53 #include <sys/jail.h>
54 
55 #include <vm/vm.h>
56 #include <vm/vm_param.h>
57 #include <vm/pmap.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_pager.h>
62 
63 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
64 
65 struct oshmctl_args;
66 static int oshmctl(struct thread *td, struct oshmctl_args *uap);
67 
68 static int shmget_allocate_segment(struct thread *td,
69     struct shmget_args *uap, int mode);
70 static int shmget_existing(struct thread *td, struct shmget_args *uap,
71     int mode, int segnum);
72 
73 /* XXX casting to (sy_call_t *) is bogus, as usual. */
74 static sy_call_t *shmcalls[] = {
75 	(sy_call_t *)shmat, (sy_call_t *)oshmctl,
76 	(sy_call_t *)shmdt, (sy_call_t *)shmget,
77 	(sy_call_t *)shmctl
78 };
79 
80 #define	SHMSEG_FREE     	0x0200
81 #define	SHMSEG_REMOVED  	0x0400
82 #define	SHMSEG_ALLOCATED	0x0800
83 #define	SHMSEG_WANTED		0x1000
84 
85 static int shm_last_free, shm_nused, shm_committed, shmalloced;
86 static struct shmid_ds	*shmsegs;
87 
88 struct shm_handle {
89 	/* vm_offset_t kva; */
90 	vm_object_t shm_object;
91 };
92 
93 struct shmmap_state {
94 	vm_offset_t va;
95 	int shmid;
96 };
97 
98 static void shm_deallocate_segment(struct shmid_ds *);
99 static int shm_find_segment_by_key(key_t);
100 static struct shmid_ds *shm_find_segment_by_shmid(int);
101 static struct shmid_ds *shm_find_segment_by_shmidx(int);
102 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
103 static void shmrealloc(void);
104 static void shminit(void);
105 static int sysvshm_modload(struct module *, int, void *);
106 static int shmunload(void);
107 static void shmexit_myhook(struct vmspace *vm);
108 static void shmfork_myhook(struct proc *p1, struct proc *p2);
109 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
110 
111 /*
112  * Tuneable values.
113  */
114 #ifndef SHMMAXPGS
115 #define	SHMMAXPGS	8192	/* Note: sysv shared memory is swap backed. */
116 #endif
117 #ifndef SHMMAX
118 #define	SHMMAX	(SHMMAXPGS*PAGE_SIZE)
119 #endif
120 #ifndef SHMMIN
121 #define	SHMMIN	1
122 #endif
123 #ifndef SHMMNI
124 #define	SHMMNI	192
125 #endif
126 #ifndef SHMSEG
127 #define	SHMSEG	128
128 #endif
129 #ifndef SHMALL
130 #define	SHMALL	(SHMMAXPGS)
131 #endif
132 
133 struct	shminfo shminfo = {
134 	SHMMAX,
135 	SHMMIN,
136 	SHMMNI,
137 	SHMSEG,
138 	SHMALL
139 };
140 
141 static int shm_use_phys;
142 static int shm_allow_removed;
143 
144 SYSCTL_DECL(_kern_ipc);
145 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0, "");
146 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0, "");
147 SYSCTL_INT(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0, "");
148 SYSCTL_INT(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0, "");
149 SYSCTL_INT(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0, "");
150 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW,
151     &shm_use_phys, 0, "");
152 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RW,
153     &shm_allow_removed, 0, "");
154 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLFLAG_RD,
155     NULL, 0, sysctl_shmsegs, "", "");
156 
157 static int
158 shm_find_segment_by_key(key)
159 	key_t key;
160 {
161 	int i;
162 
163 	for (i = 0; i < shmalloced; i++)
164 		if ((shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED) &&
165 		    shmsegs[i].shm_perm.key == key)
166 			return (i);
167 	return (-1);
168 }
169 
170 static struct shmid_ds *
171 shm_find_segment_by_shmid(int shmid)
172 {
173 	int segnum;
174 	struct shmid_ds *shmseg;
175 
176 	segnum = IPCID_TO_IX(shmid);
177 	if (segnum < 0 || segnum >= shmalloced)
178 		return (NULL);
179 	shmseg = &shmsegs[segnum];
180 	if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
181 	    (!shm_allow_removed &&
182 	     (shmseg->shm_perm.mode & SHMSEG_REMOVED) != 0) ||
183 	    shmseg->shm_perm.seq != IPCID_TO_SEQ(shmid))
184 		return (NULL);
185 	return (shmseg);
186 }
187 
188 static struct shmid_ds *
189 shm_find_segment_by_shmidx(int segnum)
190 {
191 	struct shmid_ds *shmseg;
192 
193 	if (segnum < 0 || segnum >= shmalloced)
194 		return (NULL);
195 	shmseg = &shmsegs[segnum];
196 	if ((shmseg->shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
197 	    (!shm_allow_removed &&
198 	     (shmseg->shm_perm.mode & SHMSEG_REMOVED) != 0))
199 		return (NULL);
200 	return (shmseg);
201 }
202 
203 static void
204 shm_deallocate_segment(shmseg)
205 	struct shmid_ds *shmseg;
206 {
207 	struct shm_handle *shm_handle;
208 	size_t size;
209 
210 	GIANT_REQUIRED;
211 
212 	shm_handle = shmseg->shm_internal;
213 	vm_object_deallocate(shm_handle->shm_object);
214 	free(shm_handle, M_SHM);
215 	shmseg->shm_internal = NULL;
216 	size = round_page(shmseg->shm_segsz);
217 	shm_committed -= btoc(size);
218 	shm_nused--;
219 	shmseg->shm_perm.mode = SHMSEG_FREE;
220 }
221 
222 static int
223 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
224 {
225 	struct shmid_ds *shmseg;
226 	int segnum, result;
227 	size_t size;
228 
229 	GIANT_REQUIRED;
230 
231 	segnum = IPCID_TO_IX(shmmap_s->shmid);
232 	shmseg = &shmsegs[segnum];
233 	size = round_page(shmseg->shm_segsz);
234 	result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
235 	if (result != KERN_SUCCESS)
236 		return (EINVAL);
237 	shmmap_s->shmid = -1;
238 	shmseg->shm_dtime = time_second;
239 	if ((--shmseg->shm_nattch <= 0) &&
240 	    (shmseg->shm_perm.mode & SHMSEG_REMOVED)) {
241 		shm_deallocate_segment(shmseg);
242 		shm_last_free = segnum;
243 	}
244 	return (0);
245 }
246 
247 #ifndef _SYS_SYSPROTO_H_
248 struct shmdt_args {
249 	const void *shmaddr;
250 };
251 #endif
252 
253 /*
254  * MPSAFE
255  */
256 int
257 shmdt(td, uap)
258 	struct thread *td;
259 	struct shmdt_args *uap;
260 {
261 	struct proc *p = td->td_proc;
262 	struct shmmap_state *shmmap_s;
263 	int i;
264 	int error = 0;
265 
266 	if (!jail_sysvipc_allowed && jailed(td->td_ucred))
267 		return (ENOSYS);
268 	mtx_lock(&Giant);
269 	shmmap_s = p->p_vmspace->vm_shm;
270  	if (shmmap_s == NULL) {
271 		error = EINVAL;
272 		goto done2;
273 	}
274 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
275 		if (shmmap_s->shmid != -1 &&
276 		    shmmap_s->va == (vm_offset_t)uap->shmaddr) {
277 			break;
278 		}
279 	}
280 	if (i == shminfo.shmseg) {
281 		error = EINVAL;
282 		goto done2;
283 	}
284 	error = shm_delete_mapping(p->p_vmspace, shmmap_s);
285 done2:
286 	mtx_unlock(&Giant);
287 	return (error);
288 }
289 
290 #ifndef _SYS_SYSPROTO_H_
291 struct shmat_args {
292 	int shmid;
293 	const void *shmaddr;
294 	int shmflg;
295 };
296 #endif
297 
298 /*
299  * MPSAFE
300  */
301 int
302 kern_shmat(td, shmid, shmaddr, shmflg)
303 	struct thread *td;
304 	int shmid;
305 	const void *shmaddr;
306 	int shmflg;
307 {
308 	struct proc *p = td->td_proc;
309 	int i, flags;
310 	struct shmid_ds *shmseg;
311 	struct shmmap_state *shmmap_s = NULL;
312 	struct shm_handle *shm_handle;
313 	vm_offset_t attach_va;
314 	vm_prot_t prot;
315 	vm_size_t size;
316 	int rv;
317 	int error = 0;
318 
319 	if (!jail_sysvipc_allowed && jailed(td->td_ucred))
320 		return (ENOSYS);
321 	mtx_lock(&Giant);
322 	shmmap_s = p->p_vmspace->vm_shm;
323 	if (shmmap_s == NULL) {
324 		size = shminfo.shmseg * sizeof(struct shmmap_state);
325 		shmmap_s = malloc(size, M_SHM, M_WAITOK);
326 		for (i = 0; i < shminfo.shmseg; i++)
327 			shmmap_s[i].shmid = -1;
328 		p->p_vmspace->vm_shm = shmmap_s;
329 	}
330 	shmseg = shm_find_segment_by_shmid(shmid);
331 	if (shmseg == NULL) {
332 		error = EINVAL;
333 		goto done2;
334 	}
335 	error = ipcperm(td, &shmseg->shm_perm,
336 	    (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
337 	if (error)
338 		goto done2;
339 	for (i = 0; i < shminfo.shmseg; i++) {
340 		if (shmmap_s->shmid == -1)
341 			break;
342 		shmmap_s++;
343 	}
344 	if (i >= shminfo.shmseg) {
345 		error = EMFILE;
346 		goto done2;
347 	}
348 	size = round_page(shmseg->shm_segsz);
349 #ifdef VM_PROT_READ_IS_EXEC
350 	prot = VM_PROT_READ | VM_PROT_EXECUTE;
351 #else
352 	prot = VM_PROT_READ;
353 #endif
354 	if ((shmflg & SHM_RDONLY) == 0)
355 		prot |= VM_PROT_WRITE;
356 	flags = MAP_ANON | MAP_SHARED;
357 	if (shmaddr) {
358 		flags |= MAP_FIXED;
359 		if (shmflg & SHM_RND) {
360 			attach_va = (vm_offset_t)shmaddr & ~(SHMLBA-1);
361 		} else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0) {
362 			attach_va = (vm_offset_t)shmaddr;
363 		} else {
364 			error = EINVAL;
365 			goto done2;
366 		}
367 	} else {
368 		/*
369 		 * This is just a hint to vm_map_find() about where to
370 		 * put it.
371 		 */
372 		attach_va = round_page((vm_offset_t)p->p_vmspace->vm_taddr
373 		    + maxtsiz + maxdsiz);
374 	}
375 
376 	shm_handle = shmseg->shm_internal;
377 	vm_object_reference(shm_handle->shm_object);
378 	rv = vm_map_find(&p->p_vmspace->vm_map, shm_handle->shm_object,
379 		0, &attach_va, size, (flags & MAP_FIXED)?0:1, prot, prot, 0);
380 	if (rv != KERN_SUCCESS) {
381 		error = ENOMEM;
382 		goto done2;
383 	}
384 	vm_map_inherit(&p->p_vmspace->vm_map,
385 		attach_va, attach_va + size, VM_INHERIT_SHARE);
386 
387 	shmmap_s->va = attach_va;
388 	shmmap_s->shmid = shmid;
389 	shmseg->shm_lpid = p->p_pid;
390 	shmseg->shm_atime = time_second;
391 	shmseg->shm_nattch++;
392 	td->td_retval[0] = attach_va;
393 done2:
394 	mtx_unlock(&Giant);
395 	return (error);
396 }
397 
398 int
399 shmat(td, uap)
400 	struct thread *td;
401 	struct shmat_args *uap;
402 {
403 	return kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg);
404 }
405 
406 struct oshmid_ds {
407 	struct	ipc_perm shm_perm;	/* operation perms */
408 	int	shm_segsz;		/* size of segment (bytes) */
409 	u_short	shm_cpid;		/* pid, creator */
410 	u_short	shm_lpid;		/* pid, last operation */
411 	short	shm_nattch;		/* no. of current attaches */
412 	time_t	shm_atime;		/* last attach time */
413 	time_t	shm_dtime;		/* last detach time */
414 	time_t	shm_ctime;		/* last change time */
415 	void	*shm_handle;		/* internal handle for shm segment */
416 };
417 
418 struct oshmctl_args {
419 	int shmid;
420 	int cmd;
421 	struct oshmid_ds *ubuf;
422 };
423 
424 /*
425  * MPSAFE
426  */
427 static int
428 oshmctl(td, uap)
429 	struct thread *td;
430 	struct oshmctl_args *uap;
431 {
432 #ifdef COMPAT_43
433 	int error = 0;
434 	struct shmid_ds *shmseg;
435 	struct oshmid_ds outbuf;
436 
437 	if (!jail_sysvipc_allowed && jailed(td->td_ucred))
438 		return (ENOSYS);
439 	mtx_lock(&Giant);
440 	shmseg = shm_find_segment_by_shmid(uap->shmid);
441 	if (shmseg == NULL) {
442 		error = EINVAL;
443 		goto done2;
444 	}
445 	switch (uap->cmd) {
446 	case IPC_STAT:
447 		error = ipcperm(td, &shmseg->shm_perm, IPC_R);
448 		if (error)
449 			goto done2;
450 		outbuf.shm_perm = shmseg->shm_perm;
451 		outbuf.shm_segsz = shmseg->shm_segsz;
452 		outbuf.shm_cpid = shmseg->shm_cpid;
453 		outbuf.shm_lpid = shmseg->shm_lpid;
454 		outbuf.shm_nattch = shmseg->shm_nattch;
455 		outbuf.shm_atime = shmseg->shm_atime;
456 		outbuf.shm_dtime = shmseg->shm_dtime;
457 		outbuf.shm_ctime = shmseg->shm_ctime;
458 		outbuf.shm_handle = shmseg->shm_internal;
459 		error = copyout(&outbuf, uap->ubuf, sizeof(outbuf));
460 		if (error)
461 			goto done2;
462 		break;
463 	default:
464 		/* XXX casting to (sy_call_t *) is bogus, as usual. */
465 		error = ((sy_call_t *)shmctl)(td, uap);
466 		break;
467 	}
468 done2:
469 	mtx_unlock(&Giant);
470 	return (error);
471 #else
472 	return (EINVAL);
473 #endif
474 }
475 
476 #ifndef _SYS_SYSPROTO_H_
477 struct shmctl_args {
478 	int shmid;
479 	int cmd;
480 	struct shmid_ds *buf;
481 };
482 #endif
483 
484 /*
485  * MPSAFE
486  */
487 int
488 kern_shmctl(td, shmid, cmd, buf, bufsz)
489 	struct thread *td;
490 	int shmid;
491 	int cmd;
492 	void *buf;
493 	size_t *bufsz;
494 {
495 	int error = 0;
496 	struct shmid_ds *shmseg;
497 
498 	if (!jail_sysvipc_allowed && jailed(td->td_ucred))
499 		return (ENOSYS);
500 
501 	mtx_lock(&Giant);
502 	switch (cmd) {
503 	case IPC_INFO:
504 		memcpy(buf, &shminfo, sizeof(shminfo));
505 		if (bufsz)
506 			*bufsz = sizeof(shminfo);
507 		td->td_retval[0] = shmalloced;
508 		goto done2;
509 	case SHM_INFO: {
510 		struct shm_info shm_info;
511 		shm_info.used_ids = shm_nused;
512 		shm_info.shm_rss = 0;	/*XXX where to get from ? */
513 		shm_info.shm_tot = 0;	/*XXX where to get from ? */
514 		shm_info.shm_swp = 0;	/*XXX where to get from ? */
515 		shm_info.swap_attempts = 0;	/*XXX where to get from ? */
516 		shm_info.swap_successes = 0;	/*XXX where to get from ? */
517 		memcpy(buf, &shm_info, sizeof(shm_info));
518 		if (bufsz)
519 			*bufsz = sizeof(shm_info);
520 		td->td_retval[0] = shmalloced;
521 		goto done2;
522 	}
523 	}
524 	if (cmd == SHM_STAT)
525 		shmseg = shm_find_segment_by_shmidx(shmid);
526 	else
527 		shmseg = shm_find_segment_by_shmid(shmid);
528 	if (shmseg == NULL) {
529 		error = EINVAL;
530 		goto done2;
531 	}
532 	switch (cmd) {
533 	case SHM_STAT:
534 	case IPC_STAT:
535 		error = ipcperm(td, &shmseg->shm_perm, IPC_R);
536 		if (error)
537 			goto done2;
538 		memcpy(buf, shmseg, sizeof(struct shmid_ds));
539 		if (bufsz)
540 			*bufsz = sizeof(struct shmid_ds);
541 		if (cmd == SHM_STAT)
542 			td->td_retval[0] = IXSEQ_TO_IPCID(shmid, shmseg->shm_perm);
543 		break;
544 	case IPC_SET: {
545 		struct shmid_ds *shmid;
546 
547 		shmid = (struct shmid_ds *)buf;
548 		error = ipcperm(td, &shmseg->shm_perm, IPC_M);
549 		if (error)
550 			goto done2;
551 		shmseg->shm_perm.uid = shmid->shm_perm.uid;
552 		shmseg->shm_perm.gid = shmid->shm_perm.gid;
553 		shmseg->shm_perm.mode =
554 		    (shmseg->shm_perm.mode & ~ACCESSPERMS) |
555 		    (shmid->shm_perm.mode & ACCESSPERMS);
556 		shmseg->shm_ctime = time_second;
557 		break;
558 	}
559 	case IPC_RMID:
560 		error = ipcperm(td, &shmseg->shm_perm, IPC_M);
561 		if (error)
562 			goto done2;
563 		shmseg->shm_perm.key = IPC_PRIVATE;
564 		shmseg->shm_perm.mode |= SHMSEG_REMOVED;
565 		if (shmseg->shm_nattch <= 0) {
566 			shm_deallocate_segment(shmseg);
567 			shm_last_free = IPCID_TO_IX(shmid);
568 		}
569 		break;
570 #if 0
571 	case SHM_LOCK:
572 	case SHM_UNLOCK:
573 #endif
574 	default:
575 		error = EINVAL;
576 		break;
577 	}
578 done2:
579 	mtx_unlock(&Giant);
580 	return (error);
581 }
582 
583 int
584 shmctl(td, uap)
585 	struct thread *td;
586 	struct shmctl_args *uap;
587 {
588 	int error = 0;
589 	struct shmid_ds buf;
590 	size_t bufsz;
591 
592 	/* IPC_SET needs to copyin the buffer before calling kern_shmctl */
593 	if (uap->cmd == IPC_SET) {
594 		if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
595 			goto done;
596 	}
597 
598 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
599 	if (error)
600 		goto done;
601 
602 	/* Cases in which we need to copyout */
603 	switch (uap->cmd) {
604 	case IPC_INFO:
605 	case SHM_INFO:
606 	case SHM_STAT:
607 	case IPC_STAT:
608 		error = copyout(&buf, uap->buf, bufsz);
609 		break;
610 	}
611 
612 done:
613 	if (error) {
614 		/* Invalidate the return value */
615 		td->td_retval[0] = -1;
616 	}
617 	return (error);
618 }
619 
620 
621 #ifndef _SYS_SYSPROTO_H_
622 struct shmget_args {
623 	key_t key;
624 	size_t size;
625 	int shmflg;
626 };
627 #endif
628 
629 static int
630 shmget_existing(td, uap, mode, segnum)
631 	struct thread *td;
632 	struct shmget_args *uap;
633 	int mode;
634 	int segnum;
635 {
636 	struct shmid_ds *shmseg;
637 	int error;
638 
639 	shmseg = &shmsegs[segnum];
640 	if (shmseg->shm_perm.mode & SHMSEG_REMOVED) {
641 		/*
642 		 * This segment is in the process of being allocated.  Wait
643 		 * until it's done, and look the key up again (in case the
644 		 * allocation failed or it was freed).
645 		 */
646 		shmseg->shm_perm.mode |= SHMSEG_WANTED;
647 		error = tsleep(shmseg, PLOCK | PCATCH, "shmget", 0);
648 		if (error)
649 			return (error);
650 		return (EAGAIN);
651 	}
652 	if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
653 		return (EEXIST);
654 	error = ipcperm(td, &shmseg->shm_perm, mode);
655 	if (error)
656 		return (error);
657 	if (uap->size && uap->size > shmseg->shm_segsz)
658 		return (EINVAL);
659 	td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
660 	return (0);
661 }
662 
663 static int
664 shmget_allocate_segment(td, uap, mode)
665 	struct thread *td;
666 	struct shmget_args *uap;
667 	int mode;
668 {
669 	int i, segnum, shmid, size;
670 	struct ucred *cred = td->td_ucred;
671 	struct shmid_ds *shmseg;
672 	struct shm_handle *shm_handle;
673 
674 	GIANT_REQUIRED;
675 
676 	if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
677 		return (EINVAL);
678 	if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
679 		return (ENOSPC);
680 	size = round_page(uap->size);
681 	if (shm_committed + btoc(size) > shminfo.shmall)
682 		return (ENOMEM);
683 	if (shm_last_free < 0) {
684 		shmrealloc();	/* Maybe expand the shmsegs[] array. */
685 		for (i = 0; i < shmalloced; i++)
686 			if (shmsegs[i].shm_perm.mode & SHMSEG_FREE)
687 				break;
688 		if (i == shmalloced)
689 			return (ENOSPC);
690 		segnum = i;
691 	} else  {
692 		segnum = shm_last_free;
693 		shm_last_free = -1;
694 	}
695 	shmseg = &shmsegs[segnum];
696 	/*
697 	 * In case we sleep in malloc(), mark the segment present but deleted
698 	 * so that noone else tries to create the same key.
699 	 */
700 	shmseg->shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
701 	shmseg->shm_perm.key = uap->key;
702 	shmseg->shm_perm.seq = (shmseg->shm_perm.seq + 1) & 0x7fff;
703 	shm_handle = (struct shm_handle *)
704 	    malloc(sizeof(struct shm_handle), M_SHM, M_WAITOK);
705 	shmid = IXSEQ_TO_IPCID(segnum, shmseg->shm_perm);
706 
707 	/*
708 	 * We make sure that we have allocated a pager before we need
709 	 * to.
710 	 */
711 	if (shm_use_phys) {
712 		shm_handle->shm_object =
713 		    vm_pager_allocate(OBJT_PHYS, 0, size, VM_PROT_DEFAULT, 0);
714 	} else {
715 		shm_handle->shm_object =
716 		    vm_pager_allocate(OBJT_SWAP, 0, size, VM_PROT_DEFAULT, 0);
717 	}
718 	VM_OBJECT_LOCK(shm_handle->shm_object);
719 	vm_object_clear_flag(shm_handle->shm_object, OBJ_ONEMAPPING);
720 	vm_object_set_flag(shm_handle->shm_object, OBJ_NOSPLIT);
721 	VM_OBJECT_UNLOCK(shm_handle->shm_object);
722 
723 	shmseg->shm_internal = shm_handle;
724 	shmseg->shm_perm.cuid = shmseg->shm_perm.uid = cred->cr_uid;
725 	shmseg->shm_perm.cgid = shmseg->shm_perm.gid = cred->cr_gid;
726 	shmseg->shm_perm.mode = (shmseg->shm_perm.mode & SHMSEG_WANTED) |
727 	    (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
728 	shmseg->shm_segsz = uap->size;
729 	shmseg->shm_cpid = td->td_proc->p_pid;
730 	shmseg->shm_lpid = shmseg->shm_nattch = 0;
731 	shmseg->shm_atime = shmseg->shm_dtime = 0;
732 	shmseg->shm_ctime = time_second;
733 	shm_committed += btoc(size);
734 	shm_nused++;
735 	if (shmseg->shm_perm.mode & SHMSEG_WANTED) {
736 		/*
737 		 * Somebody else wanted this key while we were asleep.  Wake
738 		 * them up now.
739 		 */
740 		shmseg->shm_perm.mode &= ~SHMSEG_WANTED;
741 		wakeup(shmseg);
742 	}
743 	td->td_retval[0] = shmid;
744 	return (0);
745 }
746 
747 /*
748  * MPSAFE
749  */
750 int
751 shmget(td, uap)
752 	struct thread *td;
753 	struct shmget_args *uap;
754 {
755 	int segnum, mode;
756 	int error;
757 
758 	if (!jail_sysvipc_allowed && jailed(td->td_ucred))
759 		return (ENOSYS);
760 	mtx_lock(&Giant);
761 	mode = uap->shmflg & ACCESSPERMS;
762 	if (uap->key != IPC_PRIVATE) {
763 	again:
764 		segnum = shm_find_segment_by_key(uap->key);
765 		if (segnum >= 0) {
766 			error = shmget_existing(td, uap, mode, segnum);
767 			if (error == EAGAIN)
768 				goto again;
769 			goto done2;
770 		}
771 		if ((uap->shmflg & IPC_CREAT) == 0) {
772 			error = ENOENT;
773 			goto done2;
774 		}
775 	}
776 	error = shmget_allocate_segment(td, uap, mode);
777 done2:
778 	mtx_unlock(&Giant);
779 	return (error);
780 }
781 
782 /*
783  * MPSAFE
784  */
785 int
786 shmsys(td, uap)
787 	struct thread *td;
788 	/* XXX actually varargs. */
789 	struct shmsys_args /* {
790 		int	which;
791 		int	a2;
792 		int	a3;
793 		int	a4;
794 	} */ *uap;
795 {
796 	int error;
797 
798 	if (!jail_sysvipc_allowed && jailed(td->td_ucred))
799 		return (ENOSYS);
800 	if (uap->which < 0 ||
801 	    uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
802 		return (EINVAL);
803 	mtx_lock(&Giant);
804 	error = (*shmcalls[uap->which])(td, &uap->a2);
805 	mtx_unlock(&Giant);
806 	return (error);
807 }
808 
809 static void
810 shmfork_myhook(p1, p2)
811 	struct proc *p1, *p2;
812 {
813 	struct shmmap_state *shmmap_s;
814 	size_t size;
815 	int i;
816 
817 	size = shminfo.shmseg * sizeof(struct shmmap_state);
818 	shmmap_s = malloc(size, M_SHM, M_WAITOK);
819 	bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
820 	p2->p_vmspace->vm_shm = shmmap_s;
821 	for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
822 		if (shmmap_s->shmid != -1)
823 			shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
824 }
825 
826 static void
827 shmexit_myhook(struct vmspace *vm)
828 {
829 	struct shmmap_state *base, *shm;
830 	int i;
831 
832 	GIANT_REQUIRED;
833 
834 	if ((base = vm->vm_shm) != NULL) {
835 		vm->vm_shm = NULL;
836 		for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
837 			if (shm->shmid != -1)
838 				shm_delete_mapping(vm, shm);
839 		}
840 		free(base, M_SHM);
841 	}
842 }
843 
844 static void
845 shmrealloc(void)
846 {
847 	int i;
848 	struct shmid_ds *newsegs;
849 
850 	if (shmalloced >= shminfo.shmmni)
851 		return;
852 
853 	newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
854 	if (newsegs == NULL)
855 		return;
856 	for (i = 0; i < shmalloced; i++)
857 		bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
858 	for (; i < shminfo.shmmni; i++) {
859 		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
860 		shmsegs[i].shm_perm.seq = 0;
861 	}
862 	free(shmsegs, M_SHM);
863 	shmsegs = newsegs;
864 	shmalloced = shminfo.shmmni;
865 }
866 
867 static void
868 shminit()
869 {
870 	int i;
871 
872 	TUNABLE_INT_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall);
873 	for (i = PAGE_SIZE; i > 0; i--) {
874 		shminfo.shmmax = shminfo.shmall * PAGE_SIZE;
875 		if (shminfo.shmmax >= shminfo.shmall)
876 			break;
877 	}
878 	TUNABLE_INT_FETCH("kern.ipc.shmmin", &shminfo.shmmin);
879 	TUNABLE_INT_FETCH("kern.ipc.shmmni", &shminfo.shmmni);
880 	TUNABLE_INT_FETCH("kern.ipc.shmseg", &shminfo.shmseg);
881 	TUNABLE_INT_FETCH("kern.ipc.shm_use_phys", &shm_use_phys);
882 
883 	shmalloced = shminfo.shmmni;
884 	shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
885 	if (shmsegs == NULL)
886 		panic("cannot allocate initial memory for sysvshm");
887 	for (i = 0; i < shmalloced; i++) {
888 		shmsegs[i].shm_perm.mode = SHMSEG_FREE;
889 		shmsegs[i].shm_perm.seq = 0;
890 	}
891 	shm_last_free = 0;
892 	shm_nused = 0;
893 	shm_committed = 0;
894 	shmexit_hook = &shmexit_myhook;
895 	shmfork_hook = &shmfork_myhook;
896 }
897 
898 static int
899 shmunload()
900 {
901 
902 	if (shm_nused > 0)
903 		return (EBUSY);
904 
905 	free(shmsegs, M_SHM);
906 	shmexit_hook = NULL;
907 	shmfork_hook = NULL;
908 	return (0);
909 }
910 
911 static int
912 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
913 {
914 
915 	return (SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0])));
916 }
917 
918 static int
919 sysvshm_modload(struct module *module, int cmd, void *arg)
920 {
921 	int error = 0;
922 
923 	switch (cmd) {
924 	case MOD_LOAD:
925 		shminit();
926 		break;
927 	case MOD_UNLOAD:
928 		error = shmunload();
929 		break;
930 	case MOD_SHUTDOWN:
931 		break;
932 	default:
933 		error = EINVAL;
934 		break;
935 	}
936 	return (error);
937 }
938 
939 static moduledata_t sysvshm_mod = {
940 	"sysvshm",
941 	&sysvshm_modload,
942 	NULL
943 };
944 
945 SYSCALL_MODULE_HELPER(shmsys);
946 SYSCALL_MODULE_HELPER(shmat);
947 SYSCALL_MODULE_HELPER(shmctl);
948 SYSCALL_MODULE_HELPER(shmdt);
949 SYSCALL_MODULE_HELPER(shmget);
950 
951 DECLARE_MODULE(sysvshm, sysvshm_mod,
952 	SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
953 MODULE_VERSION(sysvshm, 1);
954