VirtualBox

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

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

Fix

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