1 | /* $Id: alloc-r0drv-darwin.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "the-darwin-kernel.h"
|
---|
36 |
|
---|
37 | #include <iprt/alloc.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include "r0drv/alloc-r0drv.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * OS specific allocation function.
|
---|
44 | */
|
---|
45 | PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
|
---|
46 | {
|
---|
47 | PRTMEMHDR pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
|
---|
48 | if (pHdr)
|
---|
49 | {
|
---|
50 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
51 | pHdr->fFlags = fFlags;
|
---|
52 | pHdr->cb = cb;
|
---|
53 | pHdr->cbReq = cb;
|
---|
54 | }
|
---|
55 | else
|
---|
56 | printf("rmMemAlloc(%#x, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
|
---|
57 | return pHdr;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * OS specific free function.
|
---|
63 | */
|
---|
64 | void rtMemFree(PRTMEMHDR pHdr)
|
---|
65 | {
|
---|
66 | pHdr->u32Magic += 1;
|
---|
67 | IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
72 | {
|
---|
73 | /*
|
---|
74 | * validate input.
|
---|
75 | */
|
---|
76 | AssertPtr(pPhys);
|
---|
77 | Assert(cb > 0);
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * Allocate the memory and ensure that the API is still providing
|
---|
81 | * memory that's always below 4GB.
|
---|
82 | */
|
---|
83 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
84 | IOPhysicalAddress PhysAddr;
|
---|
85 | void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
|
---|
86 | if (pv)
|
---|
87 | {
|
---|
88 | if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
|
---|
89 | {
|
---|
90 | if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
|
---|
91 | {
|
---|
92 | *pPhys = PhysAddr;
|
---|
93 | return pv;
|
---|
94 | }
|
---|
95 | AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
|
---|
96 | }
|
---|
97 | else
|
---|
98 | AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
|
---|
99 | IOFreeContiguous(pv, cb);
|
---|
100 | }
|
---|
101 | return NULL;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
106 | {
|
---|
107 | if (pv)
|
---|
108 | {
|
---|
109 | Assert(cb > 0);
|
---|
110 | AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
|
---|
111 |
|
---|
112 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
113 | IOFreeContiguous(pv, cb);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|