summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/r0drv/alloc-r0drv.cpp
blob: f00f31d8883ed19402eaf703f7c9b1abd72981fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* $Id: alloc-r0drv.cpp $ */
/** @file
 * IPRT - Memory Allocation, Ring-0 Driver.
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define RTMEM_NO_WRAP_TO_EF_APIS
#include <iprt/mem.h>
#include "internal/iprt.h"

#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
# include <iprt/asm-amd64-x86.h>
#endif
#include <iprt/assert.h>
#include <iprt/err.h>
#ifdef RT_MORE_STRICT
# include <iprt/mp.h>
#endif
#include <iprt/param.h>
#include <iprt/string.h>
#include <iprt/thread.h>
#include "r0drv/alloc-r0drv.h"


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
#ifdef RT_STRICT
# define RTR0MEM_STRICT
#endif

#ifdef RTR0MEM_STRICT
# define RTR0MEM_FENCE_EXTRA    16
#else
# define RTR0MEM_FENCE_EXTRA    0
#endif


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
#ifdef RTR0MEM_STRICT
/** Fence data. */
static uint8_t const g_abFence[RTR0MEM_FENCE_EXTRA] =
{
    0x77, 0x88, 0x66, 0x99,  0x55, 0xaa, 0x44, 0xbb,
    0x33, 0xcc, 0x22, 0xdd,  0x11, 0xee, 0x00, 0xff
};
#endif


/**
 * Wrapper around rtR0MemAllocEx.
 *
 * @returns Pointer to the allocated memory block header.
 * @param   cb                  The number of bytes to allocate (sans header).
 * @param   fFlags              The allocation flags.
 */
DECLINLINE(PRTMEMHDR) rtR0MemAlloc(size_t cb, uint32_t fFlags)
{
    PRTMEMHDR pHdr;
    int rc = rtR0MemAllocEx(cb, fFlags, &pHdr);
    if (RT_FAILURE(rc))
        return NULL;
    return pHdr;
}


RTDECL(void *)  RTMemTmpAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
{
    return RTMemAllocTag(cb, pszTag);
}
RT_EXPORT_SYMBOL(RTMemTmpAllocTag);


RTDECL(void *)  RTMemTmpAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
{
    return RTMemAllocZTag(cb, pszTag);
}
RT_EXPORT_SYMBOL(RTMemTmpAllocZTag);


RTDECL(void)    RTMemTmpFree(void *pv) RT_NO_THROW_DEF
{
    return RTMemFree(pv);
}
RT_EXPORT_SYMBOL(RTMemTmpFree);


RTDECL(void)    RTMemTmpFreeZ(void *pv, size_t cb) RT_NO_THROW_DEF
{
    return RTMemFreeZ(pv, cb);
}
RT_EXPORT_SYMBOL(RTMemTmpFreeZ);





RTDECL(void *)  RTMemAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
{
    PRTMEMHDR pHdr;
    RT_ASSERT_INTS_ON();
    RT_NOREF_PV(pszTag);

    pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, 0);
    if (pHdr)
    {
#ifdef RTR0MEM_STRICT
        pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
        memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
#endif
        return pHdr + 1;
    }
    return NULL;
}
RT_EXPORT_SYMBOL(RTMemAllocTag);


RTDECL(void *)  RTMemAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
{
    PRTMEMHDR pHdr;
    RT_ASSERT_INTS_ON();
    RT_NOREF_PV(pszTag);

    pHdr = rtR0MemAlloc(cb + RTR0MEM_FENCE_EXTRA, RTMEMHDR_FLAG_ZEROED);
    if (pHdr)
    {
#ifdef RTR0MEM_STRICT
        pHdr->cbReq = (uint32_t)cb; Assert(pHdr->cbReq == cb);
        memcpy((uint8_t *)(pHdr + 1) + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
        return memset(pHdr + 1, 0, cb);
#else
        return memset(pHdr + 1, 0, pHdr->cb);
#endif
    }
    return NULL;
}
RT_EXPORT_SYMBOL(RTMemAllocZTag);


RTDECL(void *) RTMemAllocVarTag(size_t cbUnaligned, const char *pszTag)
{
    size_t cbAligned;
    if (cbUnaligned >= 16)
        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
    else
        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
    return RTMemAllocTag(cbAligned, pszTag);
}
RT_EXPORT_SYMBOL(RTMemAllocVarTag);


RTDECL(void *) RTMemAllocZVarTag(size_t cbUnaligned, const char *pszTag)
{
    size_t cbAligned;
    if (cbUnaligned >= 16)
        cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
    else
        cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
    return RTMemAllocZTag(cbAligned, pszTag);
}
RT_EXPORT_SYMBOL(RTMemAllocZVarTag);


RTDECL(void *) RTMemReallocTag(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW_DEF
{
    PRTMEMHDR pHdrOld;

    /* Free. */
    if (!cbNew && pvOld)
    {
        RTMemFree(pvOld);
        return NULL;
    }

    /* Alloc. */
    if (!pvOld)
        return RTMemAllocTag(cbNew, pszTag);

    /*
     * Realloc.
     */
    pHdrOld = (PRTMEMHDR)pvOld - 1;
    RT_ASSERT_PREEMPTIBLE();

    if (pHdrOld->u32Magic == RTMEMHDR_MAGIC)
    {
        PRTMEMHDR pHdrNew;

        /* If there is sufficient space in the old block and we don't cause
           substantial internal fragmentation, reuse the old block. */
        if (   pHdrOld->cb >= cbNew + RTR0MEM_FENCE_EXTRA
            && pHdrOld->cb - (cbNew + RTR0MEM_FENCE_EXTRA) <= 128)
        {
            pHdrOld->cbReq = (uint32_t)cbNew; Assert(pHdrOld->cbReq == cbNew);
#ifdef RTR0MEM_STRICT
            memcpy((uint8_t *)(pHdrOld + 1) + cbNew, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
#endif
            return pvOld;
        }

        /* Allocate a new block and copy over the content. */
        pHdrNew = rtR0MemAlloc(cbNew + RTR0MEM_FENCE_EXTRA, 0);
        if (pHdrNew)
        {
            size_t cbCopy = RT_MIN(pHdrOld->cb, pHdrNew->cb);
            memcpy(pHdrNew + 1, pvOld, cbCopy);
#ifdef RTR0MEM_STRICT
            pHdrNew->cbReq = (uint32_t)cbNew; Assert(pHdrNew->cbReq == cbNew);
            memcpy((uint8_t *)(pHdrNew + 1) + cbNew, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
            AssertReleaseMsg(!memcmp((uint8_t *)(pHdrOld + 1) + pHdrOld->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
                             ("pHdr=%p pvOld=%p cbReq=%u cb=%u cbNew=%zu fFlags=%#x\n"
                              "fence:    %.*Rhxs\n"
                              "expected: %.*Rhxs\n",
                              pHdrOld, pvOld, pHdrOld->cbReq, pHdrOld->cb, cbNew, pHdrOld->fFlags,
                              RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdrOld + 1) + pHdrOld->cbReq,
                              RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
#endif
            rtR0MemFree(pHdrOld);
            return pHdrNew + 1;
        }
    }
    else
        AssertMsgFailed(("pHdrOld->u32Magic=%RX32 pvOld=%p cbNew=%#zx\n", pHdrOld->u32Magic, pvOld, cbNew));

    return NULL;
}
RT_EXPORT_SYMBOL(RTMemReallocTag);


RTDECL(void) RTMemFree(void *pv) RT_NO_THROW_DEF
{
    PRTMEMHDR pHdr;
    RT_ASSERT_INTS_ON();

    if (!pv)
        return;
    pHdr = (PRTMEMHDR)pv - 1;
    if (pHdr->u32Magic == RTMEMHDR_MAGIC)
    {
        Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_ALLOC_EX));
#ifdef RTR0MEM_STRICT
        AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
                         ("pHdr=%p pv=%p cbReq=%u cb=%u fFlags=%#x\n"
                          "fence:    %.*Rhxs\n"
                          "expected: %.*Rhxs\n",
                          pHdr, pv, pHdr->cbReq, pHdr->cb, pHdr->fFlags,
                          RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cbReq,
                          RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
#endif
        rtR0MemFree(pHdr);
    }
    else
        AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
}
RT_EXPORT_SYMBOL(RTMemFree);


RTDECL(void) RTMemFreeZ(void *pv, size_t cb) RT_NO_THROW_DEF
{
    PRTMEMHDR pHdr;
    RT_ASSERT_INTS_ON();

    if (!pv)
        return;
    pHdr = (PRTMEMHDR)pv - 1;
    if (pHdr->u32Magic == RTMEMHDR_MAGIC)
    {
        Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_ALLOC_EX));
#ifdef RTR0MEM_STRICT
        AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
                         ("pHdr=%p pv=%p cbReq=%u cb=%u fFlags=%#x\n"
                          "fence:    %.*Rhxs\n"
                          "expected: %.*Rhxs\n",
                          pHdr, pv, pHdr->cbReq, pHdr->cb, pHdr->fFlags,
                          RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cbReq,
                          RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
#endif
        AssertMsgStmt(cb == pHdr->cbReq, ("cb=%#zx cbReq=%#x\n", cb, pHdr->cbReq), cb = pHdr->cbReq);
        RT_BZERO(pv, cb);
        rtR0MemFree(pHdr);
    }
    else
        AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
}
RT_EXPORT_SYMBOL(RTMemFreeZ);


RTDECL(int) RTMemAllocExTag(size_t cb, size_t cbAlignment, uint32_t fFlags, const char *pszTag, void **ppv) RT_NO_THROW_DEF
{
    uint32_t    fHdrFlags = RTMEMHDR_FLAG_ALLOC_EX;
    PRTMEMHDR   pHdr;
    int         rc;
    RT_NOREF_PV(pszTag);

    RT_ASSERT_PREEMPT_CPUID_VAR();
    if (!(fFlags & RTMEMALLOCEX_FLAGS_ANY_CTX_ALLOC))
        RT_ASSERT_INTS_ON();
    AssertReturn(!(fFlags & RTMEMALLOCEX_FLAGS_EXEC), VERR_INVALID_FLAGS);

    /*
     * Fake up some alignment support.
     */
    AssertMsgReturn(cbAlignment <= sizeof(void *), ("%zu (%#x)\n", cbAlignment, cbAlignment), VERR_UNSUPPORTED_ALIGNMENT);
    if (cb < cbAlignment)
        cb = cbAlignment;

    /*
     * Validate and convert flags.
     */
    AssertMsgReturn(!(fFlags & ~RTMEMALLOCEX_FLAGS_VALID_MASK_R0), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
    if (fFlags & RTMEMALLOCEX_FLAGS_ZEROED)
        fHdrFlags |= RTMEMHDR_FLAG_ZEROED;
    if (fFlags & RTMEMALLOCEX_FLAGS_ANY_CTX_ALLOC)
        fHdrFlags |= RTMEMHDR_FLAG_ANY_CTX_ALLOC;
    if (fFlags & RTMEMALLOCEX_FLAGS_ANY_CTX_FREE)
        fHdrFlags |= RTMEMHDR_FLAG_ANY_CTX_FREE;

    /*
     * Do the allocation.
     */
    rc = rtR0MemAllocEx(cb + RTR0MEM_FENCE_EXTRA, fHdrFlags, &pHdr);
    if (RT_SUCCESS(rc))
    {
        void *pv;

        Assert(pHdr->cbReq  == cb + RTR0MEM_FENCE_EXTRA);
        Assert((pHdr->fFlags & fFlags) == fFlags);

        /*
         * Calc user pointer, initialize the memory if requested, and if
         * memory strictness is enable set up the fence.
         */
        pv = pHdr + 1;
        *ppv = pv;
        if (fFlags & RTMEMHDR_FLAG_ZEROED)
            memset(pv, 0, pHdr->cb);

#ifdef RTR0MEM_STRICT
        pHdr->cbReq = (uint32_t)cb;
        memcpy((uint8_t *)pv + cb, &g_abFence[0], RTR0MEM_FENCE_EXTRA);
#endif
    }

    RT_ASSERT_PREEMPT_CPUID();
    return rc;
}
RT_EXPORT_SYMBOL(RTMemAllocExTag);


RTDECL(void) RTMemFreeEx(void *pv, size_t cb) RT_NO_THROW_DEF
{
    PRTMEMHDR pHdr;
    RT_NOREF_PV(cb);

    if (!pv)
        return;

    AssertPtr(pv);
    pHdr = (PRTMEMHDR)pv - 1;
    if (pHdr->u32Magic == RTMEMHDR_MAGIC)
    {
        RT_ASSERT_PREEMPT_CPUID_VAR();

        Assert(pHdr->fFlags & RTMEMHDR_FLAG_ALLOC_EX);
        if (!(pHdr->fFlags & RTMEMHDR_FLAG_ANY_CTX_FREE))
            RT_ASSERT_INTS_ON();
        AssertMsg(pHdr->cbReq == cb, ("cbReq=%zu cb=%zu\n", pHdr->cb, cb));

#ifdef RTR0MEM_STRICT
        AssertReleaseMsg(!memcmp((uint8_t *)(pHdr + 1) + pHdr->cbReq, &g_abFence[0], RTR0MEM_FENCE_EXTRA),
                         ("pHdr=%p pv=%p cbReq=%u cb=%u fFlags=%#x\n"
                          "fence:    %.*Rhxs\n"
                          "expected: %.*Rhxs\n",
                          pHdr, pv, pHdr->cbReq, pHdr->cb, pHdr->fFlags,
                          RTR0MEM_FENCE_EXTRA, (uint8_t *)(pHdr + 1) + pHdr->cbReq,
                          RTR0MEM_FENCE_EXTRA, &g_abFence[0]));
#endif
        rtR0MemFree(pHdr);
        RT_ASSERT_PREEMPT_CPUID();
    }
    else
        AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
}
RT_EXPORT_SYMBOL(RTMemFreeEx);