VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/alloc-r0drv-freebsd.c@ 29765

Last change on this file since 29765 was 29765, checked in by vboxsync, 15 years ago

*-r0drv-freebsd.c: Little cleanup and don't wire the pages twice during alloc or we will leak memory

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1/* $Id: alloc-r0drv-freebsd.c 29765 2010-05-24 19:07:28Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver, FreeBSD.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <[email protected]>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include "the-freebsd-kernel.h"
35
36#include <iprt/alloc.h>
37#include <iprt/assert.h>
38#include <iprt/param.h>
39
40#include "r0drv/alloc-r0drv.h"
41
42
43/*******************************************************************************
44* Global Variables *
45*******************************************************************************/
46/* These two statements will define two globals and add initializers
47 and destructors that will be called at load/unload time (I think). */
48MALLOC_DEFINE(M_IPRTHEAP, "iprtheap", "IPRT - heap");
49MALLOC_DEFINE(M_IPRTCONT, "iprtcont", "IPRT - contiguous");
50
51
52PRTMEMHDR rtR0MemAlloc(size_t cb, uint32_t fFlags)
53{
54 size_t cbAllocated = cb;
55 PRTMEMHDR pHdr = NULL;
56
57 /*
58 * Things are a bit more complicated on AMD64 for executable memory
59 * because we need to be in the ~2GB..~0 range for code.
60 */
61#ifdef RT_ARCH_AMD64
62 if (fFlags & RTMEMHDR_FLAG_EXEC)
63 {
64 vm_offset_t Addr = KERNBASE;
65 cbAllocated = RT_ALIGN_Z(cb + sizeof(*pHdr), PAGE_SIZE);
66
67 /* Addr contains a start address vm_map_find will start searching for suitable space at. */
68 int rc = vm_map_find(kernel_map, NULL, 0, &Addr,
69 cbAllocated,
70 TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
71 if (rc == KERN_SUCCESS)
72 {
73 /* Add the pages. */
74 vm_offset_t AddressDst = Addr;
75 bool fSuccess = true;
76
77 do
78 {
79 vm_page_t pPage;
80
81 pPage = vm_page_alloc(NULL, 0,
82 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM |
83 VM_ALLOC_WIRED | VM_ALLOC_NOOBJ);
84 if (pPage)
85 {
86 /* Put the page into the page table now. */
87 MY_PMAP_ENTER(kernel_map->pmap, AddressDst, pPage, VM_PROT_ALL,
88 TRUE);
89 }
90 else
91 {
92 /*
93 * Allocation failed. vm_map_remove will remove any
94 * page already allocated.
95 */
96 fSuccess = false;
97 break;
98 }
99 AddressDst += PAGE_SIZE;
100 } while(AddressDst < (Addr + cbAllocated));
101
102 if (fSuccess)
103 {
104 pHdr = (PRTMEMHDR)Addr;
105
106 if (fFlags & RTMEMHDR_FLAG_ZEROED)
107 bzero(pHdr, cbAllocated);
108 }
109 else
110 vm_map_remove(kernel_map,
111 Addr,
112 Addr + cb);
113 }
114 }
115 else
116#endif
117 {
118 pHdr = (PRTMEMHDR)malloc(cb + sizeof(RTMEMHDR), M_IPRTHEAP,
119 fFlags & RTMEMHDR_FLAG_ZEROED ? M_NOWAIT | M_ZERO : M_NOWAIT);
120 }
121
122 if (pHdr)
123 {
124 pHdr->u32Magic = RTMEMHDR_MAGIC;
125 pHdr->fFlags = fFlags;
126 pHdr->cb = cbAllocated;
127 pHdr->cbReq = cb;
128 return pHdr;
129 }
130 return NULL;
131}
132
133
134void rtR0MemFree(PRTMEMHDR pHdr)
135{
136 pHdr->u32Magic += 1;
137
138#ifdef RT_ARCH_AMD64
139 if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC)
140 vm_map_remove(kernel_map, (vm_offset_t)pHdr, ((vm_offset_t)pHdr) + pHdr->cb);
141 else
142#endif
143 free(pHdr, M_IPRTHEAP);
144}
145
146
147RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
148{
149 void *pv;
150
151 /*
152 * Validate input.
153 */
154 AssertPtr(pPhys);
155 Assert(cb > 0);
156
157 /*
158 * This API works in pages, so no need to do any size aligning.
159 */
160 pv = contigmalloc(cb, /* size */
161 M_IPRTCONT, /* type */
162 M_NOWAIT | M_ZERO, /* flags */
163 0, /* lowest physical address*/
164 _4G-1, /* highest physical address */
165 PAGE_SIZE, /* alignment. */
166 0); /* boundrary */
167 if (pv)
168 {
169 Assert(!((uintptr_t)pv & PAGE_OFFSET_MASK));
170 *pPhys = vtophys(pv);
171 Assert(!(*pPhys & PAGE_OFFSET_MASK));
172 }
173 return pv;
174}
175
176
177RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
178{
179 if (pv)
180 {
181 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
182 contigfree(pv, cb, M_IPRTCONT);
183 }
184}
185
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette