1 | /* $Id: mempage-native-posix.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - rtMemPageNative*, POSIX implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
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 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
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
|
---|
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.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/mem.h>
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/errcore.h>
|
---|
46 | #include <iprt/param.h>
|
---|
47 | #include "internal/mem.h"
|
---|
48 |
|
---|
49 | #include <stdlib.h>
|
---|
50 | #include <errno.h>
|
---|
51 | #include <sys/mman.h>
|
---|
52 | #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
|
---|
53 | # define MAP_ANONYMOUS MAP_ANON
|
---|
54 | #endif
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | DECLHIDDEN(int) rtMemPageNativeAlloc(size_t cb, uint32_t fFlags, void **ppvRet)
|
---|
59 | {
|
---|
60 | void *pvRet = mmap(NULL, cb,
|
---|
61 | PROT_READ | PROT_WRITE | (fFlags & RTMEMPAGEALLOC_F_EXECUTABLE ? PROT_EXEC : 0),
|
---|
62 | MAP_PRIVATE | MAP_ANONYMOUS,
|
---|
63 | -1, 0);
|
---|
64 | if (pvRet != MAP_FAILED)
|
---|
65 | {
|
---|
66 | *ppvRet = pvRet;
|
---|
67 | return VINF_SUCCESS;
|
---|
68 | }
|
---|
69 | *ppvRet = NULL;
|
---|
70 | return RTErrConvertFromErrno(errno);
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | DECLHIDDEN(int) rtMemPageNativeFree(void *pv, size_t cb)
|
---|
75 | {
|
---|
76 | int rc = munmap(pv, cb);
|
---|
77 | AssertMsgReturn(rc == 0, ("rc=%d pv=%p cb=%#zx errno=%d\n", rc, pv, cb, errno), RTErrConvertFromErrno(errno));
|
---|
78 | return VINF_SUCCESS;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | DECLHIDDEN(uint32_t) rtMemPageNativeApplyFlags(void *pv, size_t cb, uint32_t fFlags)
|
---|
83 | {
|
---|
84 | uint32_t fRet = 0;
|
---|
85 | if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
|
---|
86 | {
|
---|
87 | int rc = mlock(pv, cb);
|
---|
88 | #ifndef RT_OS_SOLARIS /* mlock(3C) on Solaris requires the priv_lock_memory privilege */
|
---|
89 | AssertMsg(rc == 0, ("mlock %p LB %#zx -> %d errno=%d\n", pv, cb, rc, errno));
|
---|
90 | #endif
|
---|
91 | if (rc == 0)
|
---|
92 | fRet |= RTMEMPAGEALLOC_F_ADVISE_LOCKED;
|
---|
93 | }
|
---|
94 |
|
---|
95 | #ifdef MADV_DONTDUMP
|
---|
96 | if (fFlags & RTMEMPAGEALLOC_F_ADVISE_NO_DUMP)
|
---|
97 | {
|
---|
98 | int rc = madvise(pv, cb, MADV_DONTDUMP);
|
---|
99 | AssertMsg(rc == 0, ("madvice %p LB %#zx MADV_DONTDUMP -> %d errno=%d\n", pv, cb, rc, errno));
|
---|
100 | if (rc == 0)
|
---|
101 | fRet |= RTMEMPAGEALLOC_F_ADVISE_NO_DUMP;
|
---|
102 | }
|
---|
103 | #endif
|
---|
104 | return fRet;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | DECLHIDDEN(void) rtMemPageNativeRevertFlags(void *pv, size_t cb, uint32_t fFlags)
|
---|
109 | {
|
---|
110 | if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
|
---|
111 | {
|
---|
112 | int rc = munlock(pv, cb);
|
---|
113 | AssertMsg(rc == 0, ("munlock %p LB %#zx -> %d errno=%d\n", pv, cb, rc, errno));
|
---|
114 | RT_NOREF(rc);
|
---|
115 | }
|
---|
116 |
|
---|
117 | #if defined(MADV_DONTDUMP) && defined(MADV_DODUMP)
|
---|
118 | if (fFlags & RTMEMPAGEALLOC_F_ADVISE_NO_DUMP)
|
---|
119 | {
|
---|
120 | int rc = madvise(pv, cb, MADV_DODUMP);
|
---|
121 | AssertMsg(rc == 0, ("madvice %p LB %#zx MADV_DODUMP -> %d errno=%d\n", pv, cb, rc, errno));
|
---|
122 | RT_NOREF(rc);
|
---|
123 | }
|
---|
124 | #endif
|
---|
125 | }
|
---|
126 |
|
---|