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