VirtualBox

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

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

iprt: build fixes

  • 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 32708 2010-09-23 11:18:51Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver, FreeBSD.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
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#include "internal/iprt.h"
36#include <iprt/mem.h>
37
38#include <iprt/assert.h>
39#include <iprt/err.h>
40#include <iprt/param.h>
41
42#include "r0drv/alloc-r0drv.h"
43
44
45/*******************************************************************************
46* Global Variables *
47*******************************************************************************/
48/* These two statements will define two globals and add initializers
49 and destructors that will be called at load/unload time (I think). */
50MALLOC_DEFINE(M_IPRTHEAP, "iprtheap", "IPRT - heap");
51MALLOC_DEFINE(M_IPRTCONT, "iprtcont", "IPRT - contiguous");
52
53
54int rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
55{
56 size_t cbAllocated = cb;
57 PRTMEMHDR pHdr = NULL;
58
59#ifdef RT_ARCH_AMD64
60 /*
61 * Things are a bit more complicated on AMD64 for executable memory
62 * because we need to be in the ~2GB..~0 range for code.
63 */
64 if (fFlags & RTMEMHDR_FLAG_EXEC)
65 {
66 if (fFlags & RTMEMHDR_FLAG_ANY_CTX)
67 return VERR_NOT_SUPPORTED;
68
69# ifdef USE_KMEM_ALLOC_PROT
70 pHdr = (PRTMEMHDR)kmem_alloc_prot(kernel_map, cb + sizeof(*pHdr),
71 VM_PROT_ALL, VM_PROT_ALL, KERNBASE);
72# else
73 vm_object_t pVmObject = NULL;
74 vm_offset_t Addr = KERNBASE;
75 cbAllocated = RT_ALIGN_Z(cb + sizeof(*pHdr), PAGE_SIZE);
76
77 pVmObject = vm_object_allocate(OBJT_DEFAULT, cbAllocated >> PAGE_SHIFT);
78 if (!pVmObject)
79 return VERR_NO_EXEC_MEMORY;
80
81 /* Addr contains a start address vm_map_find will start searching for suitable space at. */
82 int rc = vm_map_find(kernel_map, pVmObject, 0, &Addr,
83 cbAllocated, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
84 if (rc == KERN_SUCCESS)
85 {
86 rc = vm_map_wire(kernel_map, Addr, Addr + cbAllocated,
87 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
88 if (rc == KERN_SUCCESS)
89 {
90 pHdr = (PRTMEMHDR)Addr;
91
92 if (fFlags & RTMEMHDR_FLAG_ZEROED)
93 bzero(pHdr, cbAllocated);
94 }
95 else
96 vm_map_remove(kernel_map,
97 Addr,
98 Addr + cbAllocated);
99 }
100 else
101 vm_object_deallocate(pVmObject);
102# endif
103 }
104 else
105#endif
106 {
107 pHdr = (PRTMEMHDR)malloc(cb + sizeof(RTMEMHDR), M_IPRTHEAP,
108 fFlags & RTMEMHDR_FLAG_ZEROED ? M_NOWAIT | M_ZERO : M_NOWAIT);
109 }
110
111 if (RT_UNLIKELY(!pHdr))
112 return VERR_NO_MEMORY;
113
114 pHdr->u32Magic = RTMEMHDR_MAGIC;
115 pHdr->fFlags = fFlags;
116 pHdr->cb = cbAllocated;
117 pHdr->cbReq = cb;
118
119 *ppHdr = pHdr;
120 return VINF_SUCCESS;
121}
122
123
124void rtR0MemFree(PRTMEMHDR pHdr)
125{
126 pHdr->u32Magic += 1;
127
128#ifdef RT_ARCH_AMD64
129 if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC)
130# ifdef USE_KMEM_ALLOC_PROT
131 kmem_free(kernel_map, (vm_offset_t)pHdr, pHdr->cb);
132# else
133 vm_map_remove(kernel_map, (vm_offset_t)pHdr, ((vm_offset_t)pHdr) + pHdr->cb);
134# endif
135 else
136#endif
137 free(pHdr, M_IPRTHEAP);
138}
139
140
141RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
142{
143 void *pv;
144
145 /*
146 * Validate input.
147 */
148 AssertPtr(pPhys);
149 Assert(cb > 0);
150
151 /*
152 * This API works in pages, so no need to do any size aligning.
153 */
154 pv = contigmalloc(cb, /* size */
155 M_IPRTCONT, /* type */
156 M_NOWAIT | M_ZERO, /* flags */
157 0, /* lowest physical address*/
158 _4G-1, /* highest physical address */
159 PAGE_SIZE, /* alignment. */
160 0); /* boundrary */
161 if (pv)
162 {
163 Assert(!((uintptr_t)pv & PAGE_OFFSET_MASK));
164 *pPhys = vtophys(pv);
165 Assert(!(*pPhys & PAGE_OFFSET_MASK));
166 }
167 return pv;
168}
169
170
171RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
172{
173 if (pv)
174 {
175 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
176 contigfree(pv, cb, M_IPRTCONT);
177 }
178}
179
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