[33269] | 1 | /* $Id: rtmempage-exec-mmap-posix.cpp 101142 2023-09-18 11:12:16Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * IPRT - RTMemPage*, POSIX with mmap only.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[33269] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[33269] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[33269] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[33269] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[33269] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[33269] | 41 | #include "internal/iprt.h"
|
---|
| 42 | #include <iprt/mem.h>
|
---|
| 43 |
|
---|
| 44 | #include <iprt/asm.h>
|
---|
| 45 | #include <iprt/assert.h>
|
---|
[76452] | 46 | #include <iprt/errcore.h>
|
---|
[33269] | 47 | #include <iprt/param.h>
|
---|
| 48 | #include <iprt/string.h>
|
---|
[100310] | 49 | #include <iprt/system.h>
|
---|
[33269] | 50 |
|
---|
| 51 | #include <stdlib.h>
|
---|
| 52 | #include <errno.h>
|
---|
| 53 | #include <sys/mman.h>
|
---|
[33279] | 54 | #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
|
---|
| 55 | # define MAP_ANONYMOUS MAP_ANON
|
---|
| 56 | #endif
|
---|
[33269] | 57 |
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
[78334] | 60 | * Applies flags to an allocation.
|
---|
| 61 | *
|
---|
| 62 | * @param pv The allocation.
|
---|
| 63 | * @param cb The size of the allocation (page aligned).
|
---|
| 64 | * @param fFlags RTMEMPAGEALLOC_F_XXX.
|
---|
| 65 | */
|
---|
| 66 | DECLINLINE(void) rtMemPagePosixApplyFlags(void *pv, size_t cb, uint32_t fFlags)
|
---|
| 67 | {
|
---|
| 68 | #ifndef RT_OS_OS2
|
---|
| 69 | if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
|
---|
| 70 | {
|
---|
| 71 | int rc = mlock(pv, cb);
|
---|
[82660] | 72 | # ifndef RT_OS_SOLARIS /* mlock(3C) on Solaris requires the priv_lock_memory privilege */
|
---|
[78334] | 73 | AssertMsg(rc == 0, ("mlock %p LB %#zx -> %d errno=%d\n", pv, cb, rc, errno));
|
---|
[82660] | 74 | # endif
|
---|
[78334] | 75 | NOREF(rc);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | # ifdef MADV_DONTDUMP
|
---|
| 79 | if (fFlags & RTMEMPAGEALLOC_F_ADVISE_NO_DUMP)
|
---|
| 80 | {
|
---|
| 81 | int rc = madvise(pv, cb, MADV_DONTDUMP);
|
---|
| 82 | AssertMsg(rc == 0, ("madvice %p LB %#zx MADV_DONTDUMP -> %d errno=%d\n", pv, cb, rc, errno));
|
---|
| 83 | NOREF(rc);
|
---|
| 84 | }
|
---|
| 85 | # endif
|
---|
| 86 | #endif
|
---|
| 87 |
|
---|
| 88 | if (fFlags & RTMEMPAGEALLOC_F_ZERO)
|
---|
| 89 | RT_BZERO(pv, cb);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
[33269] | 94 | * Allocates memory from the specified heap.
|
---|
| 95 | *
|
---|
| 96 | * @returns Address of the allocated memory.
|
---|
| 97 | * @param cb The number of bytes to allocate.
|
---|
| 98 | * @param pszTag The tag.
|
---|
[78334] | 99 | * @param fFlags RTMEMPAGEALLOC_F_XXX.
|
---|
[33269] | 100 | * @param fProtExec PROT_EXEC or 0.
|
---|
| 101 | */
|
---|
[78334] | 102 | static void *rtMemPagePosixAlloc(size_t cb, const char *pszTag, uint32_t fFlags, int fProtExec)
|
---|
[33269] | 103 | {
|
---|
| 104 | /*
|
---|
| 105 | * Validate & adjust the input.
|
---|
| 106 | */
|
---|
| 107 | Assert(cb > 0);
|
---|
| 108 | NOREF(pszTag);
|
---|
[100310] | 109 | cb = RTSystemPageAlignSize(cb);
|
---|
[33269] | 110 |
|
---|
| 111 | /*
|
---|
| 112 | * Do the allocation.
|
---|
| 113 | */
|
---|
| 114 | void *pv = mmap(NULL, cb,
|
---|
| 115 | PROT_READ | PROT_WRITE | fProtExec,
|
---|
| 116 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
---|
| 117 | if (pv != MAP_FAILED)
|
---|
| 118 | {
|
---|
| 119 | AssertPtr(pv);
|
---|
[78334] | 120 |
|
---|
| 121 | if (fFlags)
|
---|
| 122 | rtMemPagePosixApplyFlags(pv, cb, fFlags);
|
---|
[33269] | 123 | }
|
---|
| 124 | else
|
---|
| 125 | pv = NULL;
|
---|
| 126 |
|
---|
| 127 | return pv;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
| 132 | * Free memory allocated by rtMemPagePosixAlloc.
|
---|
| 133 | *
|
---|
| 134 | * @param pv The address of the memory to free.
|
---|
| 135 | * @param cb The size.
|
---|
| 136 | */
|
---|
| 137 | static void rtMemPagePosixFree(void *pv, size_t cb)
|
---|
| 138 | {
|
---|
| 139 | /*
|
---|
| 140 | * Validate & adjust the input.
|
---|
| 141 | */
|
---|
| 142 | if (!pv)
|
---|
| 143 | return;
|
---|
| 144 | AssertPtr(pv);
|
---|
| 145 | Assert(cb > 0);
|
---|
[100310] | 146 | Assert(!((uintptr_t)pv & RTSystemGetPageOffsetMask()));
|
---|
| 147 | cb = RTSystemPageAlignSize(cb);
|
---|
[33269] | 148 |
|
---|
| 149 | /*
|
---|
| 150 | * Free the memory.
|
---|
| 151 | */
|
---|
| 152 | int rc = munmap(pv, cb);
|
---|
| 153 | AssertMsg(rc == 0, ("rc=%d pv=%p cb=%#zx\n", rc, pv, cb)); NOREF(rc);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 |
|
---|
[57432] | 160 | RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
|
---|
[33269] | 161 | {
|
---|
[78334] | 162 | return rtMemPagePosixAlloc(cb, pszTag, 0, 0);
|
---|
[33269] | 163 | }
|
---|
| 164 |
|
---|
| 165 |
|
---|
[57432] | 166 | RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
|
---|
[33269] | 167 | {
|
---|
[78334] | 168 | return rtMemPagePosixAlloc(cb, pszTag, RTMEMPAGEALLOC_F_ZERO, 0);
|
---|
[33269] | 169 | }
|
---|
| 170 |
|
---|
| 171 |
|
---|
[78334] | 172 | RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF
|
---|
| 173 | {
|
---|
[78345] | 174 | AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL);
|
---|
[101142] | 175 | return rtMemPagePosixAlloc(cb, pszTag, fFlags, !(fFlags & RTMEMPAGEALLOC_F_EXECUTABLE) ? 0 : PROT_EXEC);
|
---|
[78334] | 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
[57432] | 179 | RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF
|
---|
[33269] | 180 | {
|
---|
| 181 | return rtMemPagePosixFree(pv, cb);
|
---|
| 182 | }
|
---|
| 183 |
|
---|