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