xref: /illumos-gate/usr/src/uts/common/sys/mman.h (revision dd72704bd9e794056c558153663c739e2012d721)
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  * Copyright 2015 Joyent, Inc.  All rights reserved.
29  * Copyright 2022 Oxide Computer Company
30  */
31 
32 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
33 /*	  All Rights Reserved	*/
34 
35 /*
36  * University Copyright- Copyright (c) 1982, 1986, 1988
37  * The Regents of the University of California
38  * All Rights Reserved
39  *
40  * University Acknowledgment- Portions of this document are derived from
41  * software developed by the University of California, Berkeley, and its
42  * contributors.
43  */
44 
45 #ifndef	_SYS_MMAN_H
46 #define	_SYS_MMAN_H
47 
48 #include <sys/feature_tests.h>
49 
50 /*
51  * <sys/mman.h> has had a bit of a tortured symbol visibility history. In
52  * particular, when things were honored under __EXTENSIONS__ or not in the past
53  * wasn't very consistent. As this was not a header that was part of ISO-C it
54  * traditionally just checked around XOPEN/POSIX related feature tests. This
55  * makes the use of the standard _STRICT_POSIX something that actually is more
56  * restrictive than previously was used.
57  */
58 
59 #ifdef	__cplusplus
60 extern "C" {
61 #endif
62 
63 #if	!defined(_ASM) && !defined(_KERNEL)
64 #include <sys/types.h>
65 #endif	/* !_ASM && !_KERNEL */
66 
67 /*
68  * Protections are chosen from these bits, or-ed together.
69  * Note - not all implementations literally provide all possible
70  * combinations.  PROT_WRITE is often implemented as (PROT_READ |
71  * PROT_WRITE) and (PROT_EXECUTE as PROT_READ | PROT_EXECUTE).
72  * However, no implementation will permit a write to succeed
73  * where PROT_WRITE has not been set.  Also, no implementation will
74  * allow any access to succeed where prot is specified as PROT_NONE.
75  */
76 #define	PROT_READ	0x1		/* pages can be read */
77 #define	PROT_WRITE	0x2		/* pages can be written */
78 #define	PROT_EXEC	0x4		/* pages can be executed */
79 
80 #ifdef	_KERNEL
81 #define	PROT_USER	0x8		/* pages are user accessible */
82 #define	PROT_ZFOD	(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_USER)
83 #define	PROT_ALL	(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_USER)
84 #endif	/* _KERNEL */
85 
86 #define	PROT_NONE	0x0		/* pages cannot be accessed */
87 
88 /* sharing types:  must choose either SHARED or PRIVATE */
89 #define	MAP_SHARED	1		/* share changes */
90 #define	MAP_PRIVATE	2		/* changes are private */
91 #define	MAP_TYPE	0xf		/* mask for share type */
92 
93 /* other flags to mmap (or-ed in to MAP_SHARED or MAP_PRIVATE) */
94 #define	MAP_FILE	0		/* map from file (default) */
95 #define	MAP_FIXED	0x10		/* user assigns address */
96 /* Not implemented */
97 #define	MAP_RENAME	0x20		/* rename private pages to file */
98 #define	MAP_NORESERVE	0x40		/* don't reserve needed swap area */
99 /* Note that 0x80 is _MAP_LOW32, defined below */
100 #define	MAP_ANON	0x100		/* map anonymous pages directly */
101 #define	MAP_ANONYMOUS	MAP_ANON	/* (source compatibility) */
102 #define	MAP_ALIGN	0x200		/* addr specifies alignment */
103 #define	MAP_TEXT	0x400		/* map code segment */
104 #define	MAP_INITDATA	0x800		/* map data segment */
105 
106 /*
107  * Internal to the kernel, extensions to mmap flags.
108  */
109 #ifdef _KERNEL
110 #define	_MAP_TEXTREPL	0x1000
111 #define	_MAP_RANDOMIZE	0x2000
112 #endif /* _KERNEL */
113 
114 /*
115  * Extensions to mmap flags. These are available in the default compilation
116  * environment, but not in a strict environment.
117  */
118 #if !defined(_STRICT_POSIX)
119 #define	_MAP_LOW32	0x80	/* force mapping in lower 4G of address space */
120 #define	MAP_32BIT	_MAP_LOW32
121 
122 /*
123  * For the sake of backward object compatibility, we use the _MAP_NEW flag.
124  * This flag will be automatically or'ed in by the C library for all
125  * new mmap calls.  Previous binaries with old mmap calls will continue
126  * to get 0 or -1 for return values.  New mmap calls will get the mapped
127  * address as the return value if successful and -1 on errors.  By default,
128  * new mmap calls automatically have the kernel assign the map address
129  * unless the MAP_FIXED flag is given.
130  */
131 #define	_MAP_NEW	0x80000000	/* users should not need to use this */
132 #endif	/* !defined(_STRICT_POSIX) */
133 
134 #if !defined(_STRICT_POSIX)
135 /* External flags for mmapobj syscall (Exclusive of MAP_* flags above) */
136 #define	MMOBJ_PADDING		0x10000
137 #define	MMOBJ_INTERPRET		0x20000
138 
139 #define	MMOBJ_ALL_FLAGS		(MMOBJ_PADDING | MMOBJ_INTERPRET)
140 
141 /*
142  * Values for mr_flags field of mmapobj_result_t below.
143  * The bottom 16 bits are mutually exclusive and thus only one
144  * of them can be set at a time.  Use MR_GET_TYPE below to check this value.
145  * The top 16 bits are used for flags which are not mutually exclusive and
146  * thus more than one of these flags can be set for a given mmapobj_result_t.
147  *
148  * MR_PADDING being set indicates that this memory range represents the user
149  * requested padding.
150  *
151  * MR_HDR_ELF being set indicates that the ELF header of the mapped object
152  * is mapped at mr_addr + mr_offset.
153  */
154 
155 /*
156  * External flags for mr_flags field below.
157  */
158 #define	MR_PADDING	0x1
159 #define	MR_HDR_ELF	0x2
160 
161 /*
162  * Internal flags for mr_flags field below.
163  */
164 #ifdef	_KERNEL
165 #define	MR_RESV	0x80000000	/* overmapped /dev/null */
166 #endif	/* _KERNEL */
167 
168 #define	MR_TYPE_MASK 0x0000ffff
169 #define	MR_GET_TYPE(val)	((val) & MR_TYPE_MASK)
170 
171 #if	!defined(_ASM)
172 typedef struct mmapobj_result {
173 	caddr_t		mr_addr;	/* mapping address */
174 	size_t		mr_msize;	/* mapping size */
175 	size_t		mr_fsize;	/* file size */
176 	size_t		mr_offset;	/* offset into file */
177 	uint_t		mr_prot;	/* the protections provided */
178 	uint_t		mr_flags;	/* info on the mapping */
179 } mmapobj_result_t;
180 
181 #if defined(_KERNEL) || defined(_SYSCALL32)
182 typedef struct mmapobj_result32 {
183 	caddr32_t	mr_addr;	/* mapping address */
184 	size32_t	mr_msize;	/* mapping size */
185 	size32_t	mr_fsize;	/* file size */
186 	size32_t	mr_offset;	/* offset into file */
187 	uint_t		mr_prot;	/* the protections provided */
188 	uint_t		mr_flags;	/* info on the mapping */
189 } mmapobj_result32_t;
190 #endif	/* defined(_KERNEL) || defined(_SYSCALL32) */
191 #endif	/* !defined(_ASM) */
192 #endif	/* !defined(_STRICT_POSIX) */
193 
194 #if	!defined(_ASM) && !defined(_KERNEL)
195 /*
196  * large file compilation environment setup
197  *
198  * In the LP64 compilation environment, map large file interfaces
199  * back to native versions where possible.
200  */
201 
202 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
203 #ifdef	__PRAGMA_REDEFINE_EXTNAME
204 #pragma redefine_extname	mmap	mmap64
205 #else
206 #define	mmap			mmap64
207 #endif
208 #endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */
209 
210 #if defined(_LP64) && defined(_LARGEFILE64_SOURCE)
211 #ifdef	__PRAGMA_REDEFINE_EXTNAME
212 #pragma	redefine_extname	mmap64	mmap
213 #else
214 #define	mmap64			mmap
215 #endif
216 #endif	/* _LP64 && _LARGEFILE64_SOURCE */
217 
218 #ifdef __PRAGMA_REDEFINE_EXTNAME
219 #pragma redefine_extname	getpagesizes	getpagesizes2
220 #else
221 #define	getpagesizes	getpagesizes2
222 #endif
223 
224 /*
225  * Except for old binaries mmap() will return the resultant address of mapping
226  * on success and (void *)-1 on error.  illumos traditionally used a 'caddr_t'
227  * instead of a void * and did not require certain addresses to be const.
228  *
229  * Note, the following group of symbols are always visible since we have always
230  * exposed them and they appear to have been defined in most relevant versions
231  * of the specifications. While these are not strictly defined in ISO C, this
232  * header isn't a part of it and it isn't our job to guard against that.
233  */
234 extern void *mmap(void *, size_t, int, int, int, off_t);
235 extern int munmap(void *, size_t);
236 extern int mprotect(void *, size_t, int);
237 extern int msync(void *, size_t, int);
238 
239 #if	defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
240 	    !defined(__PRAGMA_REDEFINE_EXTNAME))
241 extern void *mmap64(void *, size_t, int, int, int, off64_t);
242 #endif  /* _LARGEFILE64_SOURCE... */
243 
244 /*
245  * These functions were all part of the older POSIX realtime suite and didn't
246  * make it into XPG until v5.
247  */
248 
249 #if !defined(_STRICT_POSIX) || (_POSIX_C_SOURCE > 2) || defined(_XPG5)
250 extern int mlock(const void *, size_t);
251 extern int munlock(const void *, size_t);
252 extern int mlockall(int);
253 extern int munlockall(void);
254 extern int shm_open(const char *, int, mode_t);
255 extern int shm_unlink(const char *);
256 #endif	/* !_STRICT_POSIX || _POSIX_C_SOURCE > 2 || _XPG5 */
257 
258 #if !defined(_STRICT_POSIX) || defined(_XPG6)
259 extern int posix_madvise(void *, size_t, int);
260 #endif
261 
262 /*
263  * The following are extensions that we have added.
264  */
265 #if !defined(_STRICT_POSIX)
266 extern int mincore(caddr_t, size_t, char *);
267 extern int memcntl(void *, size_t, int, void *, int, int);
268 extern int madvise(void *, size_t, int);
269 extern int getpagesizes(size_t *, int);
270 extern int getpagesizes2(size_t *, int);
271 extern int mmapobj(int, uint_t, mmapobj_result_t *, uint_t *, void *);
272 /* guard visibility of uint64_t */
273 #if defined(_INT64_TYPE)
274 extern int meminfo(const uint64_t *, int, const uint_t *, int, uint64_t *,
275 	uint_t *);
276 #endif /* defined(_INT64_TYPE) */
277 #endif /* !defined(_STRICT_POSIX) */
278 
279 
280 /* mmap failure value */
281 #define	MAP_FAILED	((void *) -1)
282 
283 #endif	/* !_ASM && !_KERNEL */
284 
285 #if !defined(_STRICT_POSIX)
286 #if !defined(_ASM)
287 /*
288  * structure for memcntl hat advise operations.
289  */
290 struct memcntl_mha {
291 	uint_t		mha_cmd;	/* command(s) */
292 	uint_t		mha_flags;
293 	size_t		mha_pagesize;
294 };
295 
296 #if defined(_SYSCALL32)
297 struct memcntl_mha32 {
298 	uint_t		mha_cmd;	/* command(s) */
299 	uint_t		mha_flags;
300 	size32_t	mha_pagesize;
301 };
302 #endif	/* _SYSCALL32 */
303 #endif	/* !defined(_ASM) */
304 
305 /*
306  * advice to madvise
307  *
308  * Note, if more than 4 bits worth of advice (eg. 16) are specified then
309  * changes will be necessary to the struct vpage.
310  */
311 #define	MADV_NORMAL		0	/* no further special treatment */
312 #define	MADV_RANDOM		1	/* expect random page references */
313 #define	MADV_SEQUENTIAL		2	/* expect sequential page references */
314 #define	MADV_WILLNEED		3	/* will need these pages */
315 #define	MADV_DONTNEED		4	/* don't need these pages */
316 #define	MADV_FREE		5	/* contents can be freed */
317 #define	MADV_ACCESS_DEFAULT	6	/* default access */
318 #define	MADV_ACCESS_LWP		7	/* next LWP to access heavily */
319 #define	MADV_ACCESS_MANY	8	/* many processes to access heavily */
320 #define	MADV_PURGE		9	/* contents will be purged */
321 
322 #endif	/* !defined(_STRICT_POSIX) */
323 
324 #if !defined(_STRICT_POSIX) || defined(_XPG6)
325 /* advice to posix_madvise */
326 /* these values must be kept in sync with the MADV_* values, above */
327 #define	POSIX_MADV_NORMAL	0	/* MADV_NORMAL */
328 #define	POSIX_MADV_RANDOM	1	/* MADV_RANDOM */
329 #define	POSIX_MADV_SEQUENTIAL	2	/* MADV_SEQUENTIAL */
330 #define	POSIX_MADV_WILLNEED	3	/* MADV_WILLNEED */
331 #define	POSIX_MADV_DONTNEED	4	/* MADV_DONTNEED */
332 #endif
333 
334 /* flags to msync, always visible to match the function */
335 #define	MS_OLDSYNC	0x0		/* old value of MS_SYNC */
336 					/* modified for UNIX98 compliance */
337 #define	MS_SYNC		0x4		/* wait for msync */
338 #define	MS_ASYNC	0x1		/* return immediately */
339 #define	MS_INVALIDATE	0x2		/* invalidate caches */
340 
341 #if !defined(_STRICT_POSIX) || (_POSIX_C_SOURCE > 2) || defined(_XPG5)
342 /* flags to mlockall */
343 #define	MCL_CURRENT	0x1		/* lock current mappings */
344 #define	MCL_FUTURE	0x2		/* lock future mappings */
345 #endif	/* !_STRICT_POSIX || _POSIX_C_SOURCE > 2 || _XPG5 */
346 
347 /*
348  * The following flags are older variants used by memcntl that if more generally
349  * visible under more generous rules basically conflict all over the place due
350  * to the use of common words. As such, these retain their original feature
351  * guards, as weird as they may be.
352  */
353 #if	(_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2)
354 #define	SHARED		0x10	/* Use MEMCNTL_SHARED */
355 #define	PRIVATE		0x20	/* Use MEMCNTL_PRIVATE */
356 #define	VALID_ATTR	(PROT_READ|PROT_WRITE|PROT_EXEC|SHARED|PRIVATE)
357 #endif	/* (_POSIX_C_SOURCE <= 2) && !defined(_XPG4_2) */
358 
359 #if !defined(_STRICT_POSIX)
360 /* these flags are used by memcntl */
361 #define	PROC_TEXT		(PROT_EXEC | PROT_READ)
362 #define	PROC_DATA		(PROT_READ | PROT_WRITE | PROT_EXEC)
363 #define	MEMCNTL_SHARED		0x10
364 #define	MENCNTL_PRIVATE		0x20
365 #define	MEMCNTL_VALID_ATTR	(PROT_READ |PROT_WRITE |PROT_EXEC | \
366 				    MEMCNTL_SHARED | MEMCNTL_PRIVATE)
367 
368 /* functions to memcntl */
369 #define	MC_SYNC		1		/* sync with backing store */
370 #define	MC_LOCK		2		/* lock pages in memory */
371 #define	MC_UNLOCK	3		/* unlock pages from memory */
372 #define	MC_ADVISE	4		/* give advice to management */
373 #define	MC_LOCKAS	5		/* lock address space in memory */
374 #define	MC_UNLOCKAS	6		/* unlock address space from memory */
375 #define	MC_HAT_ADVISE	7		/* advise hat map size */
376 #define	MC_INHERIT_ZERO	8		/* zero out regions on fork() */
377 
378 /* sub-commands for MC_HAT_ADVISE */
379 #define	MHA_MAPSIZE_VA		0x1	/* set preferred page size */
380 #define	MHA_MAPSIZE_BSSBRK	0x2	/* set preferred page size */
381 					/* for last bss adjacent to */
382 					/* brk area and brk area itself */
383 #define	MHA_MAPSIZE_STACK	0x4	/* set preferred page size */
384 					/* processes main stack */
385 /* definitions for meminfosys syscall */
386 #define	MISYS_MEMINFO		0x0
387 
388 #if !defined(_ASM)
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) */
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(_STRICT_POSIX) */
436 
437 #ifdef	__cplusplus
438 }
439 #endif
440 
441 #endif	/* _SYS_MMAN_H */
442