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