xref: /titanic_51/usr/src/uts/common/sys/mman.h (revision f8554bb92be6be88a684bea58293b41a6168a538)
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 2013 OmniTI Computer Consulting, Inc. All rights reserved. */
23 /*
24  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
25  *
26  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
31 /*	  All Rights Reserved	*/
32 
33 /*
34  * University Copyright- Copyright (c) 1982, 1986, 1988
35  * The Regents of the University of California
36  * All Rights Reserved
37  *
38  * University Acknowledgment- Portions of this document are derived from
39  * software developed by the University of California, Berkeley, and its
40  * contributors.
41  */
42 
43 #ifndef	_SYS_MMAN_H
44 #define	_SYS_MMAN_H
45 
46 #include <sys/feature_tests.h>
47 
48 #ifdef	__cplusplus
49 extern "C" {
50 #endif
51 
52 #if	!defined(_ASM) && !defined(_KERNEL)
53 #include <sys/types.h>
54 #endif	/* !_ASM && !_KERNEL */
55 
56 /*
57  * Protections are chosen from these bits, or-ed together.
58  * Note - not all implementations literally provide all possible
59  * combinations.  PROT_WRITE is often implemented as (PROT_READ |
60  * PROT_WRITE) and (PROT_EXECUTE as PROT_READ | PROT_EXECUTE).
61  * However, no implementation will permit a write to succeed
62  * where PROT_WRITE has not been set.  Also, no implementation will
63  * allow any access to succeed where prot is specified as PROT_NONE.
64  */
65 #define	PROT_READ	0x1		/* pages can be read */
66 #define	PROT_WRITE	0x2		/* pages can be written */
67 #define	PROT_EXEC	0x4		/* pages can be executed */
68 
69 #ifdef	_KERNEL
70 #define	PROT_USER	0x8		/* pages are user accessable */
71 #define	PROT_ZFOD	(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_USER)
72 #define	PROT_ALL	(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_USER)
73 #endif	/* _KERNEL */
74 
75 #define	PROT_NONE	0x0		/* pages cannot be accessed */
76 
77 /* sharing types:  must choose either SHARED or PRIVATE */
78 #define	MAP_SHARED	1		/* share changes */
79 #define	MAP_PRIVATE	2		/* changes are private */
80 #define	MAP_TYPE	0xf		/* mask for share type */
81 
82 /* other flags to mmap (or-ed in to MAP_SHARED or MAP_PRIVATE) */
83 #define	MAP_FIXED	0x10		/* user assigns address */
84 #define	MAP_NORESERVE	0x40		/* don't reserve needed swap area */
85 #define	MAP_ANON	0x100		/* map anonymous pages directly */
86 #define	MAP_ANONYMOUS	MAP_ANON	/* (source compatibility) */
87 #define	MAP_ALIGN	0x200		/* addr specifies alignment */
88 #define	MAP_TEXT	0x400		/* map code segment */
89 #define	MAP_INITDATA	0x800		/* map data segment */
90 
91 #ifdef _KERNEL
92 #define	_MAP_TEXTREPL	0x1000
93 #endif /* _KERNEL */
94 
95 /* these flags not yet implemented */
96 #define	MAP_RENAME	0x20		/* rename private pages to file */
97 
98 #if	(_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2)
99 /* these flags are used by memcntl */
100 #define	PROC_TEXT	(PROT_EXEC | PROT_READ)
101 #define	PROC_DATA	(PROT_READ | PROT_WRITE | PROT_EXEC)
102 #define	SHARED		0x10
103 #define	PRIVATE		0x20
104 #define	VALID_ATTR  (PROT_READ|PROT_WRITE|PROT_EXEC|SHARED|PRIVATE)
105 #endif	/* (_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2) */
106 
107 #if	(_POSIX_C_SOURCE <= 2) || defined(_XPG4_2)
108 #ifdef	_KERNEL
109 #define	PROT_EXCL	0x20
110 #endif	/* _KERNEL */
111 
112 #define	_MAP_LOW32	0x80	/* force mapping in lower 4G of address space */
113 #define	MAP_32BIT	_MAP_LOW32
114 
115 /*
116  * For the sake of backward object compatibility, we use the _MAP_NEW flag.
117  * This flag will be automatically or'ed in by the C library for all
118  * new mmap calls.  Previous binaries with old mmap calls will continue
119  * to get 0 or -1 for return values.  New mmap calls will get the mapped
120  * address as the return value if successful and -1 on errors.  By default,
121  * new mmap calls automatically have the kernel assign the map address
122  * unless the MAP_FIXED flag is given.
123  */
124 #define	_MAP_NEW	0x80000000	/* users should not need to use this */
125 #endif	/* (_POSIX_C_SOURCE <= 2) */
126 
127 
128 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
129 /* External flags for mmapobj syscall (Exclusive of MAP_* flags above) */
130 #define	MMOBJ_PADDING		0x10000
131 #define	MMOBJ_INTERPRET		0x20000
132 
133 #define	MMOBJ_ALL_FLAGS		(MMOBJ_PADDING | MMOBJ_INTERPRET)
134 
135 /*
136  * Values for mr_flags field of mmapobj_result_t below.
137  * The bottom 16 bits are mutually exclusive and thus only one
138  * of them can be set at a time.  Use MR_GET_TYPE below to check this value.
139  * The top 16 bits are used for flags which are not mutually exclusive and
140  * thus more than one of these flags can be set for a given mmapobj_result_t.
141  *
142  * MR_PADDING being set indicates that this memory range represents the user
143  * requested padding.
144  *
145  * MR_HDR_ELF being set indicates that the ELF header of the mapped object
146  * is mapped at mr_addr + mr_offset.
147  *
148  * MR_HDR_AOUT being set indicates that the AOUT (4.x) header of the mapped
149  * object is mapped at mr_addr + mr_offset.
150  */
151 
152 /*
153  * External flags for mr_flags field below.
154  */
155 #define	MR_PADDING	0x1
156 #define	MR_HDR_ELF	0x2
157 #define	MR_HDR_AOUT	0x3
158 
159 /*
160  * Internal flags for mr_flags field below.
161  */
162 #ifdef	_KERNEL
163 #define	MR_RESV	0x80000000	/* overmapped /dev/null */
164 #endif	/* _KERNEL */
165 
166 #define	MR_TYPE_MASK 0x0000ffff
167 #define	MR_GET_TYPE(val)	((val) & MR_TYPE_MASK)
168 
169 #if	!defined(_ASM)
170 typedef struct mmapobj_result {
171 	caddr_t		mr_addr;	/* mapping address */
172 	size_t		mr_msize;	/* mapping size */
173 	size_t		mr_fsize;	/* file size */
174 	size_t		mr_offset;	/* offset into file */
175 	uint_t		mr_prot;	/* the protections provided */
176 	uint_t		mr_flags;	/* info on the mapping */
177 } mmapobj_result_t;
178 
179 #if defined(_KERNEL) || defined(_SYSCALL32)
180 typedef struct mmapobj_result32 {
181 	caddr32_t	mr_addr;	/* mapping address */
182 	size32_t	mr_msize;	/* mapping size */
183 	size32_t	mr_fsize;	/* file size */
184 	size32_t	mr_offset;	/* offset into file */
185 	uint_t		mr_prot;	/* the protections provided */
186 	uint_t		mr_flags;	/* info on the mapping */
187 } mmapobj_result32_t;
188 #endif	/* defined(_KERNEL) || defined(_SYSCALL32) */
189 #endif	/* !defined(_ASM) */
190 #endif	/* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
191 
192 #if	!defined(_ASM) && !defined(_KERNEL)
193 /*
194  * large file compilation environment setup
195  *
196  * In the LP64 compilation environment, map large file interfaces
197  * back to native versions where possible.
198  */
199 
200 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
201 #ifdef	__PRAGMA_REDEFINE_EXTNAME
202 #pragma redefine_extname	mmap	mmap64
203 #else
204 #define	mmap			mmap64
205 #endif
206 #endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */
207 
208 #if defined(_LP64) && defined(_LARGEFILE64_SOURCE)
209 #ifdef	__PRAGMA_REDEFINE_EXTNAME
210 #pragma	redefine_extname	mmap64	mmap
211 #else
212 #define	mmap64			mmap
213 #endif
214 #endif	/* _LP64 && _LARGEFILE64_SOURCE */
215 
216 #ifdef __PRAGMA_REDEFINE_EXTNAME
217 #pragma redefine_extname	getpagesizes	getpagesizes2
218 #else
219 #define	getpagesizes	getpagesizes2
220 #endif
221 
222 /*
223  * Except for old binaries mmap() will return the resultant
224  * address of mapping on success and (caddr_t)-1 on error.
225  */
226 #if (_POSIX_C_SOURCE > 2) || defined(_XPG4_2)
227 extern void *mmap(void *, size_t, int, int, int, off_t);
228 extern int munmap(void *, size_t);
229 extern int mprotect(void *, size_t, int);
230 extern int msync(void *, size_t, int);
231 #if (!defined(_XPG4_2) || (_POSIX_C_SOURCE > 2)) || defined(__EXTENSIONS__)
232 extern int mlock(const void *, size_t);
233 extern int munlock(const void *, size_t);
234 #endif	/* (!defined(_XPG4_2) || (_POSIX_C_SOURCE > 2))... */
235 /* transitional large file interface version */
236 #if	defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
237 	    !defined(__PRAGMA_REDEFINE_EXTNAME))
238 extern void *mmap64(void *, size_t, int, int, int, off64_t);
239 #endif	/* _LARGEFILE64_SOURCE... */
240 #else	/* (_POSIX_C_SOURCE > 2) || defined(_XPG4_2) */
241 extern caddr_t mmap(caddr_t, size_t, int, int, int, off_t);
242 extern int munmap(caddr_t, size_t);
243 extern int mprotect(caddr_t, size_t, int);
244 extern int msync(caddr_t, size_t, int);
245 extern int mlock(caddr_t, size_t);
246 extern int munlock(caddr_t, size_t);
247 extern int mincore(caddr_t, size_t, char *);
248 extern int memcntl(caddr_t, size_t, int, caddr_t, int, int);
249 extern int madvise(caddr_t, size_t, int);
250 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
251 extern int getpagesizes(size_t *, int);
252 extern int getpagesizes2(size_t *, int);
253 extern int mmapobj(int, uint_t, mmapobj_result_t *, uint_t *, void *);
254 /* guard visibility of uint64_t */
255 #if defined(_INT64_TYPE)
256 extern int meminfo(const uint64_t *, int, const uint_t *, int, uint64_t *,
257 	uint_t *);
258 #endif /* defined(_INT64_TYPE) */
259 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
260 /* transitional large file interface version */
261 #ifdef	_LARGEFILE64_SOURCE
262 extern caddr_t mmap64(caddr_t, size_t, int, int, int, off64_t);
263 #endif
264 #endif	/* (_POSIX_C_SOURCE > 2)  || defined(_XPG4_2) */
265 
266 #if (!defined(_XPG4_2) || (_POSIX_C_SOURCE > 2)) || defined(__EXTENSIONS__)
267 extern int mlockall(int);
268 extern int munlockall(void);
269 extern int shm_open(const char *, int, mode_t);
270 extern int shm_unlink(const char *);
271 #endif
272 
273 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG6) || defined(__EXTENSIONS__)
274 extern int posix_madvise(void *, size_t, int);
275 #endif
276 
277 /* mmap failure value */
278 #define	MAP_FAILED	((void *) -1)
279 
280 
281 #endif	/* !_ASM && !_KERNEL */
282 
283 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
284 #if !defined(_ASM)
285 /*
286  * structure for memcntl hat advise operations.
287  */
288 struct memcntl_mha {
289 	uint_t 		mha_cmd;	/* command(s) */
290 	uint_t		mha_flags;
291 	size_t		mha_pagesize;
292 };
293 
294 #if defined(_SYSCALL32)
295 struct memcntl_mha32 {
296 	uint_t 		mha_cmd;	/* command(s) */
297 	uint_t		mha_flags;
298 	size32_t	mha_pagesize;
299 };
300 #endif	/* _SYSCALL32 */
301 #endif	/* !defined(_ASM) */
302 #endif	/* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
303 
304 #if	(_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2) || defined(__EXTENSIONS__)
305 /* advice to madvise */
306 #define	MADV_NORMAL		0	/* no further special treatment */
307 #define	MADV_RANDOM		1	/* expect random page references */
308 #define	MADV_SEQUENTIAL		2	/* expect sequential page references */
309 #define	MADV_WILLNEED		3	/* will need these pages */
310 #define	MADV_DONTNEED		4	/* don't need these pages */
311 #define	MADV_FREE		5	/* contents can be freed */
312 #define	MADV_ACCESS_DEFAULT	6	/* default access */
313 #define	MADV_ACCESS_LWP		7	/* next LWP to access heavily */
314 #define	MADV_ACCESS_MANY	8	/* many processes to access heavily */
315 #endif	/* (_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2) ...  */
316 
317 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG6) || defined(__EXTENSIONS__)
318 /* advice to posix_madvise */
319 /* these values must be kept in sync with the MADV_* values, above */
320 #define	POSIX_MADV_NORMAL	0	/* MADV_NORMAL */
321 #define	POSIX_MADV_RANDOM	1	/* MADV_RANDOM */
322 #define	POSIX_MADV_SEQUENTIAL	2	/* MADV_SEQUENTIAL */
323 #define	POSIX_MADV_WILLNEED	3	/* MADV_WILLNEED */
324 #define	POSIX_MADV_DONTNEED	4	/* MADV_DONTNEED */
325 #endif
326 
327 /* flags to msync */
328 #define	MS_OLDSYNC	0x0		/* old value of MS_SYNC */
329 					/* modified for UNIX98 compliance */
330 #define	MS_SYNC		0x4		/* wait for msync */
331 #define	MS_ASYNC	0x1		/* return immediately */
332 #define	MS_INVALIDATE	0x2		/* invalidate caches */
333 
334 #if	(_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2) || defined(__EXTENSIONS__)
335 /* functions to mctl */
336 #define	MC_SYNC		1		/* sync with backing store */
337 #define	MC_LOCK		2		/* lock pages in memory */
338 #define	MC_UNLOCK	3		/* unlock pages from memory */
339 #define	MC_ADVISE	4		/* give advice to management */
340 #define	MC_LOCKAS	5		/* lock address space in memory */
341 #define	MC_UNLOCKAS	6		/* unlock address space from memory */
342 #define	MC_HAT_ADVISE	7		/* advise hat map size */
343 
344 /* sub-commands for MC_HAT_ADVISE */
345 #define	MHA_MAPSIZE_VA		0x1	/* set preferred page size */
346 #define	MHA_MAPSIZE_BSSBRK	0x2	/* set preferred page size */
347 					/* for last bss adjacent to */
348 					/* brk area and brk area itself */
349 #define	MHA_MAPSIZE_STACK	0x4	/* set preferred page size */
350 					/* processes main stack */
351 
352 #endif	/* (_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2) ... */
353 
354 #if (!defined(_XPG4_2) || (_POSIX_C_SOURCE > 2)) || defined(__EXTENSIONS__)
355 /* flags to mlockall */
356 #define	MCL_CURRENT	0x1		/* lock current mappings */
357 #define	MCL_FUTURE	0x2		/* lock future mappings */
358 #endif /* (!defined(_XPG4_2) || (_POSIX_C_SOURCE)) || defined(__EXTENSIONS__) */
359 
360 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
361 
362 /* definitions for meminfosys syscall */
363 #define	MISYS_MEMINFO		0x0
364 
365 #if !defined(_ASM)
366 
367 #if defined(_INT64_TYPE)
368 /* private structure for meminfo */
369 typedef struct meminfo {
370 	const uint64_t *mi_inaddr;	/* array of input addresses */
371 	const uint_t *mi_info_req;	/* array of types of info requested */
372 	uint64_t *mi_outdata;		/* array of results are placed */
373 	uint_t *mi_validity;		/* array of bitwise result codes */
374 	int mi_info_count;		/* number of pieces of info requested */
375 } meminfo_t;
376 #endif /* defined(_INT64_TYPE) */
377 
378 #if defined(_SYSCALL32)
379 typedef struct meminfo32 {
380 	caddr32_t mi_inaddr;	/* array of input addresses */
381 	caddr32_t mi_info_req;	/* array of types of information requested */
382 	caddr32_t mi_outdata;	/* array of results are placed */
383 	caddr32_t mi_validity;	/* array of bitwise result codes */
384 	int32_t mi_info_count;	/* number of pieces of information requested */
385 } meminfo32_t;
386 #endif /* defined(_SYSCALL32) */
387 
388 #endif /* !defined(_ASM) */
389 
390 /*
391  * info_req request type definitions for meminfo
392  * request types starting with MEMINFO_V are used for Virtual addresses
393  * and should not be mixed with MEMINFO_PLGRP which is targeted for Physical
394  * addresses
395  */
396 #define	MEMINFO_SHIFT		16
397 #define	MEMINFO_MASK		(0xFF << MEMINFO_SHIFT)
398 #define	MEMINFO_VPHYSICAL	(0x01 << MEMINFO_SHIFT)	/* get physical addr */
399 #define	MEMINFO_VLGRP		(0x02 << MEMINFO_SHIFT) /* get lgroup */
400 #define	MEMINFO_VPAGESIZE	(0x03 << MEMINFO_SHIFT) /* size of phys page */
401 #define	MEMINFO_VREPLCNT	(0x04 << MEMINFO_SHIFT) /* no. of replica */
402 #define	MEMINFO_VREPL		(0x05 << MEMINFO_SHIFT) /* physical replica */
403 #define	MEMINFO_VREPL_LGRP	(0x06 << MEMINFO_SHIFT) /* lgrp of replica */
404 #define	MEMINFO_PLGRP		(0x07 << MEMINFO_SHIFT) /* lgroup for paddr */
405 
406 /* maximum number of addresses meminfo() can process at a time */
407 #define	MAX_MEMINFO_CNT	256
408 
409 /* maximum number of request types */
410 #define	MAX_MEMINFO_REQ	31
411 
412 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
413 
414 #ifdef	__cplusplus
415 }
416 #endif
417 
418 #endif	/* _SYS_MMAN_H */
419