xref: /minix3/sys/sys/shm.h (revision 4d272e5a970c4e7d6700cabd52e337e37404b70e)
10a6a1f1dSLionel Sambuc /*	$NetBSD: shm.h,v 1.49 2015/05/13 01:16:15 pgoyette Exp $	*/
2f6aac1c3SLionel Sambuc 
3f6aac1c3SLionel Sambuc /*-
4f6aac1c3SLionel Sambuc  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5f6aac1c3SLionel Sambuc  * All rights reserved.
6f6aac1c3SLionel Sambuc  *
7f6aac1c3SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8f6aac1c3SLionel Sambuc  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9f6aac1c3SLionel Sambuc  * NASA Ames Research Center.
10f6aac1c3SLionel Sambuc  *
11f6aac1c3SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
12f6aac1c3SLionel Sambuc  * modification, are permitted provided that the following conditions
13f6aac1c3SLionel Sambuc  * are met:
14f6aac1c3SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
15f6aac1c3SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
16f6aac1c3SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
17f6aac1c3SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
18f6aac1c3SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
19f6aac1c3SLionel Sambuc  *
20f6aac1c3SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21f6aac1c3SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22f6aac1c3SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23f6aac1c3SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24f6aac1c3SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25f6aac1c3SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26f6aac1c3SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27f6aac1c3SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28f6aac1c3SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29f6aac1c3SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30f6aac1c3SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
31f6aac1c3SLionel Sambuc  */
32f6aac1c3SLionel Sambuc 
33f6aac1c3SLionel Sambuc /*
34f6aac1c3SLionel Sambuc  * Copyright (c) 1994 Adam Glass
35f6aac1c3SLionel Sambuc  * All rights reserved.
36f6aac1c3SLionel Sambuc  *
37f6aac1c3SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
38f6aac1c3SLionel Sambuc  * modification, are permitted provided that the following conditions
39f6aac1c3SLionel Sambuc  * are met:
40f6aac1c3SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
41f6aac1c3SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
42f6aac1c3SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
43f6aac1c3SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
44f6aac1c3SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
45f6aac1c3SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
46f6aac1c3SLionel Sambuc  *    must display the following acknowledgement:
47f6aac1c3SLionel Sambuc  *      This product includes software developed by Adam Glass.
48f6aac1c3SLionel Sambuc  * 4. The name of the author may not be used to endorse or promote products
49f6aac1c3SLionel Sambuc  *    derived from this software without specific prior written permission
50f6aac1c3SLionel Sambuc  *
51f6aac1c3SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52f6aac1c3SLionel Sambuc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53f6aac1c3SLionel Sambuc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54f6aac1c3SLionel Sambuc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55f6aac1c3SLionel Sambuc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56f6aac1c3SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57f6aac1c3SLionel Sambuc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58f6aac1c3SLionel Sambuc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59f6aac1c3SLionel Sambuc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60f6aac1c3SLionel Sambuc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61f6aac1c3SLionel Sambuc  */
62f6aac1c3SLionel Sambuc 
63f6aac1c3SLionel Sambuc /*
64f6aac1c3SLionel Sambuc  * As defined+described in "X/Open System Interfaces and Headers"
65f6aac1c3SLionel Sambuc  *                         Issue 4, p. XXX
66f6aac1c3SLionel Sambuc  */
67f6aac1c3SLionel Sambuc 
68f6aac1c3SLionel Sambuc #ifndef _SYS_SHM_H_
69f6aac1c3SLionel Sambuc #define _SYS_SHM_H_
70f6aac1c3SLionel Sambuc 
71f6aac1c3SLionel Sambuc #include <sys/cdefs.h>
72f6aac1c3SLionel Sambuc #include <sys/featuretest.h>
73f6aac1c3SLionel Sambuc 
74f6aac1c3SLionel Sambuc #include <sys/ipc.h>
75f6aac1c3SLionel Sambuc 
76f6aac1c3SLionel Sambuc #define	SHM_RDONLY	010000	/* Attach read-only (else read-write) */
77f6aac1c3SLionel Sambuc #define	SHM_RND		020000	/* Round attach address to SHMLBA */
78515ae0a3SBen Gras #ifdef _KERNEL
79515ae0a3SBen Gras #define _SHM_RMLINGER	040000	/* Attach even if segment removed */
80515ae0a3SBen Gras #endif
81f6aac1c3SLionel Sambuc 
82f6aac1c3SLionel Sambuc /* Segment low boundry address multiple */
83515ae0a3SBen Gras #if defined(_KERNEL) || defined(_STANDALONE) || defined(_MODULE)
84515ae0a3SBen Gras #define	SHMLBA		PAGE_SIZE
85515ae0a3SBen Gras #else
86515ae0a3SBen Gras /*
87515ae0a3SBen Gras  * SHMLBA uses libc's internal __sysconf() to retrieve the machine's
88515ae0a3SBen Gras  * page size. The value of _SC_PAGESIZE is 28 -- we hard code it so we do not
89515ae0a3SBen Gras  * need to include unistd.h
90515ae0a3SBen Gras  */
91515ae0a3SBen Gras __BEGIN_DECLS
92515ae0a3SBen Gras long __sysconf(int);
93515ae0a3SBen Gras __END_DECLS
94515ae0a3SBen Gras #define	SHMLBA		(__sysconf(28))
95515ae0a3SBen Gras #endif
96f6aac1c3SLionel Sambuc 
97f6aac1c3SLionel Sambuc typedef unsigned int	shmatt_t;
98f6aac1c3SLionel Sambuc 
99f6aac1c3SLionel Sambuc struct shmid_ds {
100f6aac1c3SLionel Sambuc 	struct ipc_perm	shm_perm;	/* operation permission structure */
101f6aac1c3SLionel Sambuc 	size_t		shm_segsz;	/* size of segment in bytes */
102515ae0a3SBen Gras 	pid_t		shm_lpid;	/* process ID of last shm operation */
103515ae0a3SBen Gras 	pid_t		shm_cpid;	/* process ID of creator */
104515ae0a3SBen Gras 	shmatt_t	shm_nattch;	/* number of current attaches */
105f6aac1c3SLionel Sambuc 	time_t		shm_atime;	/* time of last shmat() */
106f6aac1c3SLionel Sambuc 	time_t		shm_dtime;	/* time of last shmdt() */
107f6aac1c3SLionel Sambuc 	time_t		shm_ctime;	/* time of last change by shmctl() */
108515ae0a3SBen Gras 
109515ae0a3SBen Gras 	/*
110515ae0a3SBen Gras 	 * These members are private and used only in the internal
111515ae0a3SBen Gras 	 * implementation of this interface.
112515ae0a3SBen Gras 	 */
113515ae0a3SBen Gras 	void		*_shm_internal;
114f6aac1c3SLionel Sambuc };
115f6aac1c3SLionel Sambuc 
116515ae0a3SBen Gras #if defined(_NETBSD_SOURCE)
117515ae0a3SBen Gras /*
118515ae0a3SBen Gras  * Some systems (e.g. HP-UX) take these as the second (cmd) arg to shmctl().
119515ae0a3SBen Gras  */
120515ae0a3SBen Gras #define	SHM_LOCK	3	/* Lock segment in memory. */
121515ae0a3SBen Gras #define	SHM_UNLOCK	4	/* Unlock a segment locked by SHM_LOCK. */
122515ae0a3SBen Gras #endif /* _NETBSD_SOURCE */
123f6aac1c3SLionel Sambuc 
124515ae0a3SBen Gras #if defined(_NETBSD_SOURCE)
125f6aac1c3SLionel Sambuc /*
126f6aac1c3SLionel Sambuc  * Permission definitions used in shmflag arguments to shmat(2) and shmget(2).
127f6aac1c3SLionel Sambuc  * Provided for source compatibility only; do not use in new code!
128f6aac1c3SLionel Sambuc  */
129515ae0a3SBen Gras #define	SHM_R		IPC_R	/* S_IRUSR, R for owner */
130515ae0a3SBen Gras #define	SHM_W		IPC_W	/* S_IWUSR, W for owner */
131f6aac1c3SLionel Sambuc 
132f6aac1c3SLionel Sambuc /*
133f6aac1c3SLionel Sambuc  * System 5 style catch-all structure for shared memory constants that
134f6aac1c3SLionel Sambuc  * might be of interest to user programs.  Do we really want/need this?
135f6aac1c3SLionel Sambuc  */
136f6aac1c3SLionel Sambuc struct shminfo {
137515ae0a3SBen Gras 	uint64_t	shmmax;	/* max shared memory segment size (bytes) */
138515ae0a3SBen Gras 	uint32_t	shmmin;	/* min shared memory segment size (bytes) */
139515ae0a3SBen Gras 	uint32_t	shmmni;	/* max number of shared memory identifiers */
140515ae0a3SBen Gras 	uint32_t	shmseg;	/* max shared memory segments per process */
141515ae0a3SBen Gras 	uint32_t	shmall;	/* max amount of shared memory (pages) */
142f6aac1c3SLionel Sambuc };
143f6aac1c3SLionel Sambuc 
144515ae0a3SBen Gras /* Warning: 64-bit structure padding is needed here */
145515ae0a3SBen Gras struct shmid_ds_sysctl {
146515ae0a3SBen Gras 	struct		ipc_perm_sysctl shm_perm;
147515ae0a3SBen Gras 	uint64_t	shm_segsz;
148515ae0a3SBen Gras 	pid_t		shm_lpid;
149515ae0a3SBen Gras 	pid_t		shm_cpid;
150515ae0a3SBen Gras 	time_t		shm_atime;
151515ae0a3SBen Gras 	time_t		shm_dtime;
152515ae0a3SBen Gras 	time_t		shm_ctime;
153515ae0a3SBen Gras 	uint32_t	shm_nattch;
154515ae0a3SBen Gras };
155515ae0a3SBen Gras struct shm_sysctl_info {
156515ae0a3SBen Gras 	struct	shminfo shminfo;
157515ae0a3SBen Gras 	struct	shmid_ds_sysctl shmids[1];
158515ae0a3SBen Gras };
159515ae0a3SBen Gras #endif /* _NETBSD_SOURCE */
160515ae0a3SBen Gras 
161515ae0a3SBen Gras #ifdef _KERNEL
162515ae0a3SBen Gras extern struct shminfo shminfo;
163515ae0a3SBen Gras extern struct shmid_ds *shmsegs;
164515ae0a3SBen Gras extern int shm_nused;
165515ae0a3SBen Gras 
166515ae0a3SBen Gras #define	SHMSEG_FREE		0x0200
167515ae0a3SBen Gras #define	SHMSEG_REMOVED		0x0400
168515ae0a3SBen Gras #define	SHMSEG_ALLOCATED	0x0800
169515ae0a3SBen Gras #define	SHMSEG_WANTED		0x1000
170515ae0a3SBen Gras #define	SHMSEG_RMLINGER		0x2000
171515ae0a3SBen Gras #define	SHMSEG_WIRED		0x4000
172515ae0a3SBen Gras 
173515ae0a3SBen Gras struct vmspace;
174515ae0a3SBen Gras 
175515ae0a3SBen Gras void	shminit(void);
1760a6a1f1dSLionel Sambuc int	shmfini(void);
177515ae0a3SBen Gras void	shmfork(struct vmspace *, struct vmspace *);
178515ae0a3SBen Gras void	shmexit(struct vmspace *);
179515ae0a3SBen Gras int	shmctl1(struct lwp *, int, int, struct shmid_ds *);
180515ae0a3SBen Gras 
181515ae0a3SBen Gras #define SYSCTL_FILL_SHM(src, dst) do { \
182515ae0a3SBen Gras 	SYSCTL_FILL_PERM((src).shm_perm, (dst).shm_perm); \
183515ae0a3SBen Gras 	(dst).shm_segsz = (src).shm_segsz; \
184515ae0a3SBen Gras 	(dst).shm_lpid = (src).shm_lpid; \
185515ae0a3SBen Gras 	(dst).shm_cpid = (src).shm_cpid; \
186515ae0a3SBen Gras 	(dst).shm_atime = (src).shm_atime; \
187515ae0a3SBen Gras 	(dst).shm_dtime = (src).shm_dtime; \
188515ae0a3SBen Gras 	(dst).shm_ctime = (src).shm_ctime; \
189515ae0a3SBen Gras 	(dst).shm_nattch = (src).shm_nattch; \
190515ae0a3SBen Gras } while (/*CONSTCOND*/ 0)
191515ae0a3SBen Gras 
192515ae0a3SBen Gras #else /* !_KERNEL */
193515ae0a3SBen Gras 
194515ae0a3SBen Gras __BEGIN_DECLS
195515ae0a3SBen Gras void	*shmat(int, const void *, int);
196515ae0a3SBen Gras int	shmctl(int, int, struct shmid_ds *) __RENAME(__shmctl50);
197515ae0a3SBen Gras int	shmdt(const void *);
198515ae0a3SBen Gras int	shmget(key_t, size_t, int);
199515ae0a3SBen Gras __END_DECLS
200515ae0a3SBen Gras 
201515ae0a3SBen Gras #endif /* !_KERNEL */
202515ae0a3SBen Gras 
20384d9c625SLionel Sambuc #if defined(__minix)
204515ae0a3SBen Gras /* ipcs ctl commands */
205515ae0a3SBen Gras #define SHM_STAT       13
206515ae0a3SBen Gras #define SHM_INFO       14
207515ae0a3SBen Gras 
208f6aac1c3SLionel Sambuc struct shm_info
209f6aac1c3SLionel Sambuc {
210f6aac1c3SLionel Sambuc        int used_ids;
211f6aac1c3SLionel Sambuc        unsigned long int shm_tot;  /* total allocated shm */
212f6aac1c3SLionel Sambuc        unsigned long int shm_rss;  /* total resident shm */
213f6aac1c3SLionel Sambuc        unsigned long int shm_swp;  /* total swapped shm */
214f6aac1c3SLionel Sambuc        unsigned long int swap_attempts;
215f6aac1c3SLionel Sambuc        unsigned long int swap_successes;
216f6aac1c3SLionel Sambuc };
217f6aac1c3SLionel Sambuc 
218*4d272e5aSDavid van Moolenbroek #define SHMMNI 1024
219515ae0a3SBen Gras #define SHMSEG 32              /* max shared segs per process */
220f6aac1c3SLionel Sambuc 
221*4d272e5aSDavid van Moolenbroek /* Public shm_perm.mode flags, synchronized with NetBSD kernel values above */
222*4d272e5aSDavid van Moolenbroek #define SHM_DEST	0x0400	/* destroy on last detach (SHMSEG_REMOVED) */
223*4d272e5aSDavid van Moolenbroek #define SHM_LOCKED	0x4000	/* pages will not be swapped (SHMSEG_WIRED) */
224515ae0a3SBen Gras 
22584d9c625SLionel Sambuc #endif /* defined(__minix) */
226f6aac1c3SLionel Sambuc 
227f6aac1c3SLionel Sambuc #endif /* !_SYS_SHM_H_ */
228