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