VirtualBox

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

最後變更 在這個檔案從52822是 49718,由 vboxsync 提交於 11 年 前

Various FreeBSD fixes submitted Bernhard Froehlich

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 5.9 KB
 
1/* $Id: alloc-r0drv-freebsd.c 49718 2013-11-29 10:51:54Z 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#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
54DECLHIDDEN(int) 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#if __FreeBSD_version >= 1000055
83 int rc = vm_map_find(kernel_map, pVmObject, 0, &Addr,
84 cbAllocated, 0, VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL, 0);
85#else
86 int rc = vm_map_find(kernel_map, pVmObject, 0, &Addr,
87 cbAllocated, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
88#endif
89 if (rc == KERN_SUCCESS)
90 {
91 rc = vm_map_wire(kernel_map, Addr, Addr + cbAllocated,
92 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
93 if (rc == KERN_SUCCESS)
94 {
95 pHdr = (PRTMEMHDR)Addr;
96
97 if (fFlags & RTMEMHDR_FLAG_ZEROED)
98 bzero(pHdr, cbAllocated);
99 }
100 else
101 vm_map_remove(kernel_map,
102 Addr,
103 Addr + cbAllocated);
104 }
105 else
106 vm_object_deallocate(pVmObject);
107# endif
108 }
109 else
110#endif
111 {
112 pHdr = (PRTMEMHDR)malloc(cb + sizeof(RTMEMHDR), M_IPRTHEAP,
113 fFlags & RTMEMHDR_FLAG_ZEROED ? M_NOWAIT | M_ZERO : M_NOWAIT);
114 }
115
116 if (RT_UNLIKELY(!pHdr))
117 return VERR_NO_MEMORY;
118
119 pHdr->u32Magic = RTMEMHDR_MAGIC;
120 pHdr->fFlags = fFlags;
121 pHdr->cb = cbAllocated;
122 pHdr->cbReq = cb;
123
124 *ppHdr = pHdr;
125 return VINF_SUCCESS;
126}
127
128
129DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
130{
131 pHdr->u32Magic += 1;
132
133#ifdef RT_ARCH_AMD64
134 if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC)
135# ifdef USE_KMEM_ALLOC_PROT
136 kmem_free(kernel_map, (vm_offset_t)pHdr, pHdr->cb);
137# else
138 vm_map_remove(kernel_map, (vm_offset_t)pHdr, ((vm_offset_t)pHdr) + pHdr->cb);
139# endif
140 else
141#endif
142 free(pHdr, M_IPRTHEAP);
143}
144
145
146RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
147{
148 void *pv;
149
150 /*
151 * Validate input.
152 */
153 AssertPtr(pPhys);
154 Assert(cb > 0);
155
156 /*
157 * This API works in pages, so no need to do any size aligning.
158 */
159 pv = contigmalloc(cb, /* size */
160 M_IPRTCONT, /* type */
161 M_NOWAIT | M_ZERO, /* flags */
162 0, /* lowest physical address*/
163 _4G-1, /* highest physical address */
164 PAGE_SIZE, /* alignment. */
165 0); /* boundary */
166 if (pv)
167 {
168 Assert(!((uintptr_t)pv & PAGE_OFFSET_MASK));
169 *pPhys = vtophys(pv);
170 Assert(!(*pPhys & PAGE_OFFSET_MASK));
171 }
172 return pv;
173}
174
175
176RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
177{
178 if (pv)
179 {
180 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
181 contigfree(pv, cb, M_IPRTCONT);
182 }
183}
184
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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