VirtualBox

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

Last change on this file since 32707 was 32707, checked in by vboxsync, 14 years ago

IPRT: Added RTMemAllocEx[Tag] and RTMemFreeEx, only implemented in ring-0 only.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/* $Id: alloc-r0drv-freebsd.c 32707 2010-09-23 10:15:08Z 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
52int rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
53{
54 size_t cbAllocated = cb;
55 PRTMEMHDR pHdr = NULL;
56
57#ifdef RT_ARCH_AMD64
58 /*
59 * Things are a bit more complicated on AMD64 for executable memory
60 * because we need to be in the ~2GB..~0 range for code.
61 */
62 if (fFlags & RTMEMHDR_FLAG_EXEC)
63 {
64 if (fFlags & RTMEMHDR_FLAG_ANY_CTX)
65 return VERR_NOT_SUPPORTED;
66
67# ifdef USE_KMEM_ALLOC_PROT
68 pHdr = (PRTMEMHDR)kmem_alloc_prot(kernel_map, cb + sizeof(*pHdr),
69 VM_PROT_ALL, VM_PROT_ALL, KERNBASE);
70# else
71 vm_object_t pVmObject = NULL;
72 vm_offset_t Addr = KERNBASE;
73 cbAllocated = RT_ALIGN_Z(cb + sizeof(*pHdr), PAGE_SIZE);
74
75 pVmObject = vm_object_allocate(OBJT_DEFAULT, cbAllocated >> PAGE_SHIFT);
76 if (!pVmObject)
77 return VERR_NO_EXEC_MEMORY;
78
79 /* Addr contains a start address vm_map_find will start searching for suitable space at. */
80 int rc = vm_map_find(kernel_map, pVmObject, 0, &Addr,
81 cbAllocated, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
82 if (rc == KERN_SUCCESS)
83 {
84 rc = vm_map_wire(kernel_map, Addr, Addr + cbAllocated,
85 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
86 if (rc == KERN_SUCCESS)
87 {
88 pHdr = (PRTMEMHDR)Addr;
89
90 if (fFlags & RTMEMHDR_FLAG_ZEROED)
91 bzero(pHdr, cbAllocated);
92 }
93 else
94 vm_map_remove(kernel_map,
95 Addr,
96 Addr + cbAllocated);
97 }
98 else
99 vm_object_deallocate(pVmObject);
100# endif
101 }
102 else
103#endif
104 {
105 pHdr = (PRTMEMHDR)malloc(cb + sizeof(RTMEMHDR), M_IPRTHEAP,
106 fFlags & RTMEMHDR_FLAG_ZEROED ? M_NOWAIT | M_ZERO : M_NOWAIT);
107 }
108
109 if (RT_UNLIKELY(!pHdr))
110 return VERR_NO_MEMORY;
111
112 pHdr->u32Magic = RTMEMHDR_MAGIC;
113 pHdr->fFlags = fFlags;
114 pHdr->cb = cbAllocated;
115 pHdr->cbReq = cb;
116
117 *ppHdr = pHdr;
118 return VINF_SUCCESS;
119}
120
121
122void rtR0MemFree(PRTMEMHDR pHdr)
123{
124 pHdr->u32Magic += 1;
125
126#ifdef RT_ARCH_AMD64
127 if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC)
128# ifdef USE_KMEM_ALLOC_PROT
129 kmem_free(kernel_map, (vm_offset_t)pHdr, pHdr->cb);
130# else
131 vm_map_remove(kernel_map, (vm_offset_t)pHdr, ((vm_offset_t)pHdr) + pHdr->cb);
132# endif
133 else
134#endif
135 free(pHdr, M_IPRTHEAP);
136}
137
138
139RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
140{
141 void *pv;
142
143 /*
144 * Validate input.
145 */
146 AssertPtr(pPhys);
147 Assert(cb > 0);
148
149 /*
150 * This API works in pages, so no need to do any size aligning.
151 */
152 pv = contigmalloc(cb, /* size */
153 M_IPRTCONT, /* type */
154 M_NOWAIT | M_ZERO, /* flags */
155 0, /* lowest physical address*/
156 _4G-1, /* highest physical address */
157 PAGE_SIZE, /* alignment. */
158 0); /* boundrary */
159 if (pv)
160 {
161 Assert(!((uintptr_t)pv & PAGE_OFFSET_MASK));
162 *pPhys = vtophys(pv);
163 Assert(!(*pPhys & PAGE_OFFSET_MASK));
164 }
165 return pv;
166}
167
168
169RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
170{
171 if (pv)
172 {
173 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
174 contigfree(pv, cb, M_IPRTCONT);
175 }
176}
177
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