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