xref: /illumos-gate/usr/src/uts/common/sys/mman.h (revision d362b7492b8bcb5ed70f92aa0a6a39bc93b059de)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved	*/
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 #ifndef	_SYS_MMAN_H
40 #define	_SYS_MMAN_H
41 
42 #pragma ident	"%Z%%M%	%I%	%E% SMI"
43 
44 #include <sys/feature_tests.h>
45 
46 #ifdef	__cplusplus
47 extern "C" {
48 #endif
49 
50 /*
51  * Protections are chosen from these bits, or-ed together.
52  * Note - not all implementations literally provide all possible
53  * combinations.  PROT_WRITE is often implemented as (PROT_READ |
54  * PROT_WRITE) and (PROT_EXECUTE as PROT_READ | PROT_EXECUTE).
55  * However, no implementation will permit a write to succeed
56  * where PROT_WRITE has not been set.  Also, no implementation will
57  * allow any access to succeed where prot is specified as PROT_NONE.
58  */
59 #define	PROT_READ	0x1		/* pages can be read */
60 #define	PROT_WRITE	0x2		/* pages can be written */
61 #define	PROT_EXEC	0x4		/* pages can be executed */
62 
63 #ifdef	_KERNEL
64 #define	PROT_USER	0x8		/* pages are user accessable */
65 #define	PROT_ZFOD	(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_USER)
66 #define	PROT_ALL	(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_USER)
67 #endif	/* _KERNEL */
68 
69 #define	PROT_NONE	0x0		/* pages cannot be accessed */
70 
71 /* sharing types:  must choose either SHARED or PRIVATE */
72 #define	MAP_SHARED	1		/* share changes */
73 #define	MAP_PRIVATE	2		/* changes are private */
74 #define	MAP_TYPE	0xf		/* mask for share type */
75 
76 /* other flags to mmap (or-ed in to MAP_SHARED or MAP_PRIVATE) */
77 #define	MAP_FIXED	0x10		/* user assigns address */
78 #define	MAP_NORESERVE	0x40		/* don't reserve needed swap area */
79 #define	MAP_ANON	0x100		/* map anonymous pages directly */
80 #define	MAP_ANONYMOUS	MAP_ANON	/* (source compatibility) */
81 #define	MAP_ALIGN	0x200		/* addr specifies alignment */
82 #define	MAP_TEXT	0x400		/* map code segment */
83 #define	MAP_INITDATA	0x800		/* map data segment */
84 
85 #ifdef _KERNEL
86 #define	_MAP_TEXTREPL	0x1000
87 #endif /* _KERNEL */
88 
89 /* these flags not yet implemented */
90 #define	MAP_RENAME	0x20		/* rename private pages to file */
91 
92 #if	(_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2)
93 /* these flags are used by memcntl */
94 #define	PROC_TEXT	(PROT_EXEC | PROT_READ)
95 #define	PROC_DATA	(PROT_READ | PROT_WRITE | PROT_EXEC)
96 #define	SHARED		0x10
97 #define	PRIVATE		0x20
98 #define	VALID_ATTR  (PROT_READ|PROT_WRITE|PROT_EXEC|SHARED|PRIVATE)
99 #endif	/* (_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2) */
100 
101 #if	(_POSIX_C_SOURCE <= 2) || defined(_XPG4_2)
102 #ifdef	_KERNEL
103 #define	PROT_EXCL	0x20
104 #define	_MAP_LOW32	0x80	/* force mapping in lower 4G of address space */
105 #endif	/* _KERNEL */
106 
107 /*
108  * For the sake of backward object compatibility, we use the _MAP_NEW flag.
109  * This flag will be automatically or'ed in by the C library for all
110  * new mmap calls.  Previous binaries with old mmap calls will continue
111  * to get 0 or -1 for return values.  New mmap calls will get the mapped
112  * address as the return value if successful and -1 on errors.  By default,
113  * new mmap calls automatically have the kernel assign the map address
114  * unless the MAP_FIXED flag is given.
115  */
116 #define	_MAP_NEW	0x80000000	/* users should not need to use this */
117 #endif	/* (_POSIX_C_SOURCE <= 2) */
118 
119 #if	!defined(_ASM) && !defined(_KERNEL)
120 
121 #include <sys/types.h>
122 
123 /*
124  * large file compilation environment setup
125  *
126  * In the LP64 compilation environment, map large file interfaces
127  * back to native versions where possible.
128  */
129 
130 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
131 #ifdef	__PRAGMA_REDEFINE_EXTNAME
132 #pragma redefine_extname	mmap	mmap64
133 #else
134 #define	mmap			mmap64
135 #endif
136 #endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */
137 
138 #if defined(_LP64) && defined(_LARGEFILE64_SOURCE)
139 #ifdef	__PRAGMA_REDEFINE_EXTNAME
140 #pragma	redefine_extname	mmap64	mmap
141 #else
142 #define	mmap64			mmap
143 #endif
144 #endif	/* _LP64 && _LARGEFILE64_SOURCE */
145 
146 /*
147  * Except for old binaries mmap() will return the resultant
148  * address of mapping on success and (caddr_t)-1 on error.
149  */
150 #ifdef	__STDC__
151 #if (_POSIX_C_SOURCE > 2) || defined(_XPG4_2)
152 extern void *mmap(void *, size_t, int, int, int, off_t);
153 extern int munmap(void *, size_t);
154 extern int mprotect(void *, size_t, int);
155 extern int msync(void *, size_t, int);
156 #if (!defined(_XPG4_2) || (_POSIX_C_SOURCE > 2)) || defined(__EXTENSIONS__)
157 extern int mlock(const void *, size_t);
158 extern int munlock(const void *, size_t);
159 #endif	/* (!defined(_XPG4_2) || (_POSIX_C_SOURCE > 2))... */
160 /* transitional large file interface version */
161 #if	defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
162 	    !defined(__PRAGMA_REDEFINE_EXTNAME))
163 extern void *mmap64(void *, size_t, int, int, int, off64_t);
164 #endif	/* _LARGEFILE64_SOURCE... */
165 #else	/* (_POSIX_C_SOURCE > 2) || defined(_XPG4_2) */
166 extern caddr_t mmap(caddr_t, size_t, int, int, int, off_t);
167 extern int munmap(caddr_t, size_t);
168 extern int mprotect(caddr_t, size_t, int);
169 extern int msync(caddr_t, size_t, int);
170 extern int mlock(caddr_t, size_t);
171 extern int munlock(caddr_t, size_t);
172 extern int mincore(caddr_t, size_t, char *);
173 extern int memcntl(caddr_t, size_t, int, caddr_t, int, int);
174 extern int madvise(caddr_t, size_t, int);
175 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
176 extern int getpagesizes(size_t *, int);
177 /* guard visibility of uint64_t */
178 #if defined(_INT64_TYPE)
179 extern int meminfo(const uint64_t *, int, const uint_t *, int, uint64_t *,
180 	uint_t *);
181 #endif /* defined(_INT64_TYPE) */
182 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
183 /* transitional large file interface version */
184 #ifdef	_LARGEFILE64_SOURCE
185 extern caddr_t mmap64(caddr_t, size_t, int, int, int, off64_t);
186 #endif
187 #endif	/* (_POSIX_C_SOURCE > 2)  || defined(_XPG4_2) */
188 
189 #if (!defined(_XPG4_2) || (_POSIX_C_SOURCE > 2)) || defined(__EXTENSIONS__)
190 extern int mlockall(int);
191 extern int munlockall(void);
192 extern int shm_open(const char *, int, mode_t);
193 extern int shm_unlink(const char *);
194 #endif
195 
196 /* mmap failure value */
197 #define	MAP_FAILED	((void *) -1)
198 
199 #else	/* __STDC__ */
200 extern caddr_t mmap();
201 extern int munmap();
202 extern int mprotect();
203 extern int mincore();
204 extern int memcntl();
205 extern int msync();
206 extern int madvise();
207 extern int getpagesizes();
208 extern int mlock();
209 extern int mlockall();
210 extern int munlock();
211 extern int munlockall();
212 extern int meminfo();
213 extern int shm_open();
214 extern int shm_unlink();
215 
216 /* transitional large file interface version */
217 #if	defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
218 	    !defined(__PRAGMA_REDEFINE_EXTNAME))
219 extern caddr_t mmap64();
220 #endif	/* _LARGEFILE64_SOURCE... */
221 #endif	/* __STDC__ */
222 
223 
224 #endif	/* !_ASM && !_KERNEL */
225 
226 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
227 #if !defined(_ASM)
228 /*
229  * structure for memcntl hat advise operations.
230  */
231 struct memcntl_mha {
232 	uint_t 		mha_cmd;	/* command(s) */
233 	uint_t		mha_flags;
234 	size_t		mha_pagesize;
235 };
236 
237 #if defined(_SYSCALL32)
238 struct memcntl_mha32 {
239 	uint_t 		mha_cmd;	/* command(s) */
240 	uint_t		mha_flags;
241 	size32_t	mha_pagesize;
242 };
243 #endif	/* _SYSCALL32 */
244 #endif	/* !defined(_ASM) */
245 #endif	/* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
246 
247 #if	(_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2) || defined(__EXTENSIONS__)
248 /* advice to madvise */
249 #define	MADV_NORMAL		0	/* no further special treatment */
250 #define	MADV_RANDOM		1	/* expect random page references */
251 #define	MADV_SEQUENTIAL		2	/* expect sequential page references */
252 #define	MADV_WILLNEED		3	/* will need these pages */
253 #define	MADV_DONTNEED		4	/* don't need these pages */
254 #define	MADV_FREE		5	/* contents can be freed */
255 #define	MADV_ACCESS_DEFAULT	6	/* default access */
256 #define	MADV_ACCESS_LWP		7	/* next LWP to access heavily */
257 #define	MADV_ACCESS_MANY	8	/* many processes to access heavily */
258 #endif	/* (_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2) ...  */
259 
260 /* flags to msync */
261 #define	MS_OLDSYNC	0x0		/* old value of MS_SYNC */
262 					/* modified for UNIX98 compliance */
263 #define	MS_SYNC		0x4		/* wait for msync */
264 #define	MS_ASYNC	0x1		/* return immediately */
265 #define	MS_INVALIDATE	0x2		/* invalidate caches */
266 
267 #if	(_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2) || defined(__EXTENSIONS__)
268 /* functions to mctl */
269 #define	MC_SYNC		1		/* sync with backing store */
270 #define	MC_LOCK		2		/* lock pages in memory */
271 #define	MC_UNLOCK	3		/* unlock pages from memory */
272 #define	MC_ADVISE	4		/* give advice to management */
273 #define	MC_LOCKAS	5		/* lock address space in memory */
274 #define	MC_UNLOCKAS	6		/* unlock address space from memory */
275 #define	MC_HAT_ADVISE	7		/* advise hat map size */
276 
277 /* sub-commands for MC_HAT_ADVISE */
278 #define	MHA_MAPSIZE_VA		0x1	/* set preferred page size */
279 #define	MHA_MAPSIZE_BSSBRK	0x2	/* set preferred page size */
280 					/* for last bss adjacent to */
281 					/* brk area and brk area itself */
282 #define	MHA_MAPSIZE_STACK	0x4	/* set preferred page size */
283 					/* processes main stack */
284 
285 #endif	/* (_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2) ... */
286 
287 #if (!defined(_XPG4_2) || (_POSIX_C_SOURCE > 2)) || defined(__EXTENSIONS__)
288 /* flags to mlockall */
289 #define	MCL_CURRENT	0x1		/* lock current mappings */
290 #define	MCL_FUTURE	0x2		/* lock future mappings */
291 #endif /* (!defined(_XPG4_2) || (_POSIX_C_SOURCE)) || defined(__EXTENSIONS__) */
292 
293 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
294 
295 /* definitions for meminfosys syscall */
296 #define	MISYS_MEMINFO		0x0
297 
298 #if !defined(_ASM) && defined(__STDC__)
299 
300 #if defined(_INT64_TYPE)
301 /* private structure for meminfo */
302 typedef struct meminfo {
303 	const uint64_t *mi_inaddr;	/* array of input addresses */
304 	const uint_t *mi_info_req;	/* array of types of info requested */
305 	uint64_t *mi_outdata;		/* array of results are placed */
306 	uint_t *mi_validity;		/* array of bitwise result codes */
307 	int mi_info_count;		/* number of pieces of info requested */
308 } meminfo_t;
309 #endif /* defined(_INT64_TYPE) */
310 
311 #if defined(_SYSCALL32)
312 typedef struct meminfo32 {
313 	caddr32_t mi_inaddr;	/* array of input addresses */
314 	caddr32_t mi_info_req;	/* array of types of information requested */
315 	caddr32_t mi_outdata;	/* array of results are placed */
316 	caddr32_t mi_validity;	/* array of bitwise result codes */
317 	int32_t mi_info_count;	/* number of pieces of information requested */
318 } meminfo32_t;
319 #endif /* defined(_SYSCALL32) */
320 
321 #endif /* !defined(_ASM) && defined(__STDC__) */
322 
323 /*
324  * info_req request type definitions for meminfo
325  * request types starting with MEMINFO_V are used for Virtual addresses
326  * and should not be mixed with MEMINFO_PLGRP which is targeted for Physical
327  * addresses
328  */
329 #define	MEMINFO_SHIFT		16
330 #define	MEMINFO_MASK		(0xFF << MEMINFO_SHIFT)
331 #define	MEMINFO_VPHYSICAL	(0x01 << MEMINFO_SHIFT)	/* get physical addr */
332 #define	MEMINFO_VLGRP		(0x02 << MEMINFO_SHIFT) /* get lgroup */
333 #define	MEMINFO_VPAGESIZE	(0x03 << MEMINFO_SHIFT) /* size of phys page */
334 #define	MEMINFO_VREPLCNT	(0x04 << MEMINFO_SHIFT) /* no. of replica */
335 #define	MEMINFO_VREPL		(0x05 << MEMINFO_SHIFT) /* physical replica */
336 #define	MEMINFO_VREPL_LGRP	(0x06 << MEMINFO_SHIFT) /* lgrp of replica */
337 #define	MEMINFO_PLGRP		(0x07 << MEMINFO_SHIFT) /* lgroup for paddr */
338 
339 /* maximum number of addresses meminfo() can process at a time */
340 #define	MAX_MEMINFO_CNT	256
341 
342 /* maximum number of request types */
343 #define	MAX_MEMINFO_REQ	31
344 
345 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
346 
347 #ifdef	__cplusplus
348 }
349 #endif
350 
351 #endif	/* _SYS_MMAN_H */
352