1 | /* $Id: alloc-r0drv-freebsd.c 57358 2015-08-14 15:16:38Z 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 | /*********************************************************************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *********************************************************************************************************************************/
|
---|
35 | #include "the-freebsd-kernel.h"
|
---|
36 | #include "internal/iprt.h"
|
---|
37 | #include <iprt/mem.h>
|
---|
38 |
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/param.h>
|
---|
42 |
|
---|
43 | #include "r0drv/alloc-r0drv.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Global Variables *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 | /* These two statements will define two globals and add initializers
|
---|
50 | and destructors that will be called at load/unload time (I think). */
|
---|
51 | MALLOC_DEFINE(M_IPRTHEAP, "iprtheap", "IPRT - heap");
|
---|
52 | MALLOC_DEFINE(M_IPRTCONT, "iprtcont", "IPRT - contiguous");
|
---|
53 |
|
---|
54 |
|
---|
55 | DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
|
---|
56 | {
|
---|
57 | size_t cbAllocated = cb;
|
---|
58 | PRTMEMHDR pHdr = NULL;
|
---|
59 |
|
---|
60 | #ifdef RT_ARCH_AMD64
|
---|
61 | /*
|
---|
62 | * Things are a bit more complicated on AMD64 for executable memory
|
---|
63 | * because we need to be in the ~2GB..~0 range for code.
|
---|
64 | */
|
---|
65 | if (fFlags & RTMEMHDR_FLAG_EXEC)
|
---|
66 | {
|
---|
67 | if (fFlags & RTMEMHDR_FLAG_ANY_CTX)
|
---|
68 | return VERR_NOT_SUPPORTED;
|
---|
69 |
|
---|
70 | # ifdef USE_KMEM_ALLOC_PROT
|
---|
71 | pHdr = (PRTMEMHDR)kmem_alloc_prot(kernel_map, cb + sizeof(*pHdr),
|
---|
72 | VM_PROT_ALL, VM_PROT_ALL, KERNBASE);
|
---|
73 | # else
|
---|
74 | vm_object_t pVmObject = NULL;
|
---|
75 | vm_offset_t Addr = KERNBASE;
|
---|
76 | cbAllocated = RT_ALIGN_Z(cb + sizeof(*pHdr), PAGE_SIZE);
|
---|
77 |
|
---|
78 | pVmObject = vm_object_allocate(OBJT_DEFAULT, cbAllocated >> PAGE_SHIFT);
|
---|
79 | if (!pVmObject)
|
---|
80 | return VERR_NO_EXEC_MEMORY;
|
---|
81 |
|
---|
82 | /* Addr contains a start address vm_map_find will start searching for suitable space at. */
|
---|
83 | #if __FreeBSD_version >= 1000055
|
---|
84 | int rc = vm_map_find(kernel_map, pVmObject, 0, &Addr,
|
---|
85 | cbAllocated, 0, VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL, 0);
|
---|
86 | #else
|
---|
87 | int rc = vm_map_find(kernel_map, pVmObject, 0, &Addr,
|
---|
88 | cbAllocated, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
|
---|
89 | #endif
|
---|
90 | if (rc == KERN_SUCCESS)
|
---|
91 | {
|
---|
92 | rc = vm_map_wire(kernel_map, Addr, Addr + cbAllocated,
|
---|
93 | VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
|
---|
94 | if (rc == KERN_SUCCESS)
|
---|
95 | {
|
---|
96 | pHdr = (PRTMEMHDR)Addr;
|
---|
97 |
|
---|
98 | if (fFlags & RTMEMHDR_FLAG_ZEROED)
|
---|
99 | bzero(pHdr, cbAllocated);
|
---|
100 | }
|
---|
101 | else
|
---|
102 | vm_map_remove(kernel_map,
|
---|
103 | Addr,
|
---|
104 | Addr + cbAllocated);
|
---|
105 | }
|
---|
106 | else
|
---|
107 | vm_object_deallocate(pVmObject);
|
---|
108 | # endif
|
---|
109 | }
|
---|
110 | else
|
---|
111 | #endif
|
---|
112 | {
|
---|
113 | pHdr = (PRTMEMHDR)malloc(cb + sizeof(RTMEMHDR), M_IPRTHEAP,
|
---|
114 | fFlags & RTMEMHDR_FLAG_ZEROED ? M_NOWAIT | M_ZERO : M_NOWAIT);
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (RT_UNLIKELY(!pHdr))
|
---|
118 | return VERR_NO_MEMORY;
|
---|
119 |
|
---|
120 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
121 | pHdr->fFlags = fFlags;
|
---|
122 | pHdr->cb = cbAllocated;
|
---|
123 | pHdr->cbReq = cb;
|
---|
124 |
|
---|
125 | *ppHdr = pHdr;
|
---|
126 | return VINF_SUCCESS;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
|
---|
131 | {
|
---|
132 | pHdr->u32Magic += 1;
|
---|
133 |
|
---|
134 | #ifdef RT_ARCH_AMD64
|
---|
135 | if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC)
|
---|
136 | # ifdef USE_KMEM_ALLOC_PROT
|
---|
137 | kmem_free(kernel_map, (vm_offset_t)pHdr, pHdr->cb);
|
---|
138 | # else
|
---|
139 | vm_map_remove(kernel_map, (vm_offset_t)pHdr, ((vm_offset_t)pHdr) + pHdr->cb);
|
---|
140 | # endif
|
---|
141 | else
|
---|
142 | #endif
|
---|
143 | free(pHdr, M_IPRTHEAP);
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | RTR0DECL(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); /* boundary */
|
---|
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 |
|
---|
177 | RTR0DECL(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 |
|
---|