[1] | 1 | /* $Id: alloc-r0drv-darwin.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Memory Allocation, Ring-0 Driver, Darwin.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 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 | *
|
---|
[5999] | 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
|
---|
[5999] | 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
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[63451] | 41 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" /* (IOMallocContiguous et al are deprecated) */
|
---|
[1] | 42 | #include "the-darwin-kernel.h"
|
---|
[22052] | 43 | #include "internal/iprt.h"
|
---|
| 44 | #include <iprt/mem.h>
|
---|
[41054] | 45 | #include <iprt/memobj.h>
|
---|
[1] | 46 |
|
---|
| 47 | #include <iprt/assert.h>
|
---|
[76452] | 48 | #include <iprt/errcore.h>
|
---|
[22052] | 49 | #include <iprt/thread.h>
|
---|
[1] | 50 | #include "r0drv/alloc-r0drv.h"
|
---|
| 51 |
|
---|
| 52 |
|
---|
[41054] | 53 |
|
---|
| 54 | /**
|
---|
[1] | 55 | * OS specific allocation function.
|
---|
| 56 | */
|
---|
[36555] | 57 | DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
|
---|
[1] | 58 | {
|
---|
[57228] | 59 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[97910] | 60 | if (RT_LIKELY(!(fFlags & RTMEMHDR_FLAG_ANY_CTX)))
|
---|
[1] | 61 | {
|
---|
[97910] | 62 | PRTMEMHDR pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
|
---|
| 63 | if (RT_LIKELY(pHdr))
|
---|
[57228] | 64 | {
|
---|
[97910] | 65 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
| 66 | pHdr->fFlags = fFlags;
|
---|
| 67 | pHdr->cb = cb;
|
---|
| 68 | pHdr->cbReq = cb;
|
---|
| 69 | *ppHdr = pHdr;
|
---|
| 70 |
|
---|
[57228] | 71 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[97910] | 72 | return VINF_SUCCESS;
|
---|
[57228] | 73 | }
|
---|
[97910] | 74 |
|
---|
| 75 | printf("rtR0MemAllocEx(%#zx, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
|
---|
| 76 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
| 77 | return VERR_NO_MEMORY;
|
---|
[1] | 78 | }
|
---|
[57228] | 79 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[97910] | 80 | return VERR_NOT_SUPPORTED;
|
---|
[1] | 81 | }
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * OS specific free function.
|
---|
| 86 | */
|
---|
[36555] | 87 | DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
|
---|
[1] | 88 | {
|
---|
[57228] | 89 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
| 90 |
|
---|
[1] | 91 | pHdr->u32Magic += 1;
|
---|
[97910] | 92 | IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
|
---|
[57228] | 93 |
|
---|
| 94 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[1] | 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
| 99 | {
|
---|
| 100 | /*
|
---|
| 101 | * validate input.
|
---|
| 102 | */
|
---|
| 103 | AssertPtr(pPhys);
|
---|
| 104 | Assert(cb > 0);
|
---|
[22052] | 105 | RT_ASSERT_PREEMPTIBLE();
|
---|
[57074] | 106 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[1] | 107 |
|
---|
| 108 | /*
|
---|
| 109 | * Allocate the memory and ensure that the API is still providing
|
---|
| 110 | * memory that's always below 4GB.
|
---|
| 111 | */
|
---|
| 112 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
| 113 | IOPhysicalAddress PhysAddr;
|
---|
| 114 | void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
|
---|
| 115 | if (pv)
|
---|
| 116 | {
|
---|
| 117 | if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
|
---|
| 118 | {
|
---|
| 119 | if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
|
---|
| 120 | {
|
---|
| 121 | *pPhys = PhysAddr;
|
---|
[57074] | 122 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[1] | 123 | return pv;
|
---|
| 124 | }
|
---|
| 125 | AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
|
---|
| 126 | }
|
---|
| 127 | else
|
---|
| 128 | AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
|
---|
| 129 | IOFreeContiguous(pv, cb);
|
---|
| 130 | }
|
---|
[57074] | 131 |
|
---|
| 132 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[1] | 133 | return NULL;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
| 138 | {
|
---|
[22052] | 139 | RT_ASSERT_PREEMPTIBLE();
|
---|
[1] | 140 | if (pv)
|
---|
| 141 | {
|
---|
| 142 | Assert(cb > 0);
|
---|
| 143 | AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
|
---|
[57074] | 144 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
[1] | 145 |
|
---|
| 146 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
| 147 | IOFreeContiguous(pv, cb);
|
---|
[57074] | 148 |
|
---|
| 149 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
[1] | 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|