1 | /* $Id: alloc-r0drv-freebsd.c 77120 2019-02-01 15:08:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, FreeBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Contributed by knut st. osmundsen.
|
---|
8 | *
|
---|
9 | * Copyright (C) 2007-2019 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * The contents of this file may alternatively be used under the terms
|
---|
20 | * of the Common Development and Distribution License Version 1.0
|
---|
21 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
22 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
23 | * CDDL are applicable instead of those of the GPL.
|
---|
24 | *
|
---|
25 | * You may elect to license modified versions of this file under the
|
---|
26 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
27 | *
|
---|
28 | * --------------------------------------------------------------------
|
---|
29 | *
|
---|
30 | * This code is based on:
|
---|
31 | *
|
---|
32 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
33 | *
|
---|
34 | * Permission is hereby granted, free of charge, to any person
|
---|
35 | * obtaining a copy of this software and associated documentation
|
---|
36 | * files (the "Software"), to deal in the Software without
|
---|
37 | * restriction, including without limitation the rights to use,
|
---|
38 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
39 | * copies of the Software, and to permit persons to whom the
|
---|
40 | * Software is furnished to do so, subject to the following
|
---|
41 | * conditions:
|
---|
42 | *
|
---|
43 | * The above copyright notice and this permission notice shall be
|
---|
44 | * included in all copies or substantial portions of the Software.
|
---|
45 | *
|
---|
46 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
47 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
48 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
49 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
50 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
51 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
52 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
53 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
54 | */
|
---|
55 |
|
---|
56 |
|
---|
57 | /*********************************************************************************************************************************
|
---|
58 | * Header Files *
|
---|
59 | *********************************************************************************************************************************/
|
---|
60 | #include "the-freebsd-kernel.h"
|
---|
61 | #include "internal/iprt.h"
|
---|
62 | #include <iprt/mem.h>
|
---|
63 |
|
---|
64 | #include <iprt/assert.h>
|
---|
65 | #include <iprt/err.h>
|
---|
66 | #include <iprt/param.h>
|
---|
67 |
|
---|
68 | #include "r0drv/alloc-r0drv.h"
|
---|
69 |
|
---|
70 |
|
---|
71 | /*********************************************************************************************************************************
|
---|
72 | * Global Variables *
|
---|
73 | *********************************************************************************************************************************/
|
---|
74 | /* These two statements will define two globals and add initializers
|
---|
75 | and destructors that will be called at load/unload time (I think). */
|
---|
76 | MALLOC_DEFINE(M_IPRTHEAP, "iprtheap", "IPRT - heap");
|
---|
77 | MALLOC_DEFINE(M_IPRTCONT, "iprtcont", "IPRT - contiguous");
|
---|
78 |
|
---|
79 |
|
---|
80 | DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
|
---|
81 | {
|
---|
82 | size_t cbAllocated = cb;
|
---|
83 | PRTMEMHDR pHdr = NULL;
|
---|
84 |
|
---|
85 | #ifdef RT_ARCH_AMD64
|
---|
86 | /*
|
---|
87 | * Things are a bit more complicated on AMD64 for executable memory
|
---|
88 | * because we need to be in the ~2GB..~0 range for code.
|
---|
89 | */
|
---|
90 | if (fFlags & RTMEMHDR_FLAG_EXEC)
|
---|
91 | {
|
---|
92 | if (fFlags & RTMEMHDR_FLAG_ANY_CTX)
|
---|
93 | return VERR_NOT_SUPPORTED;
|
---|
94 |
|
---|
95 | # ifdef USE_KMEM_ALLOC_PROT
|
---|
96 | pHdr = (PRTMEMHDR)kmem_alloc_prot(kernel_map, cb + sizeof(*pHdr),
|
---|
97 | VM_PROT_ALL, VM_PROT_ALL, KERNBASE);
|
---|
98 | # else
|
---|
99 | vm_object_t pVmObject = NULL;
|
---|
100 | vm_offset_t Addr = KERNBASE;
|
---|
101 | cbAllocated = RT_ALIGN_Z(cb + sizeof(*pHdr), PAGE_SIZE);
|
---|
102 |
|
---|
103 | pVmObject = vm_object_allocate(OBJT_DEFAULT, cbAllocated >> PAGE_SHIFT);
|
---|
104 | if (!pVmObject)
|
---|
105 | return VERR_NO_EXEC_MEMORY;
|
---|
106 |
|
---|
107 | /* Addr contains a start address vm_map_find will start searching for suitable space at. */
|
---|
108 | #if __FreeBSD_version >= 1000055
|
---|
109 | int rc = vm_map_find(kernel_map, pVmObject, 0, &Addr,
|
---|
110 | cbAllocated, 0, VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL, 0);
|
---|
111 | #else
|
---|
112 | int rc = vm_map_find(kernel_map, pVmObject, 0, &Addr,
|
---|
113 | cbAllocated, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
|
---|
114 | #endif
|
---|
115 | if (rc == KERN_SUCCESS)
|
---|
116 | {
|
---|
117 | rc = vm_map_wire(kernel_map, Addr, Addr + cbAllocated,
|
---|
118 | VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
|
---|
119 | if (rc == KERN_SUCCESS)
|
---|
120 | {
|
---|
121 | pHdr = (PRTMEMHDR)Addr;
|
---|
122 |
|
---|
123 | if (fFlags & RTMEMHDR_FLAG_ZEROED)
|
---|
124 | bzero(pHdr, cbAllocated);
|
---|
125 | }
|
---|
126 | else
|
---|
127 | vm_map_remove(kernel_map,
|
---|
128 | Addr,
|
---|
129 | Addr + cbAllocated);
|
---|
130 | }
|
---|
131 | else
|
---|
132 | vm_object_deallocate(pVmObject);
|
---|
133 | # endif
|
---|
134 | }
|
---|
135 | else
|
---|
136 | #endif
|
---|
137 | {
|
---|
138 | pHdr = (PRTMEMHDR)malloc(cb + sizeof(RTMEMHDR), M_IPRTHEAP,
|
---|
139 | fFlags & RTMEMHDR_FLAG_ZEROED ? M_NOWAIT | M_ZERO : M_NOWAIT);
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (RT_UNLIKELY(!pHdr))
|
---|
143 | return VERR_NO_MEMORY;
|
---|
144 |
|
---|
145 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
146 | pHdr->fFlags = fFlags;
|
---|
147 | pHdr->cb = cbAllocated;
|
---|
148 | pHdr->cbReq = cb;
|
---|
149 |
|
---|
150 | *ppHdr = pHdr;
|
---|
151 | return VINF_SUCCESS;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
|
---|
156 | {
|
---|
157 | pHdr->u32Magic += 1;
|
---|
158 |
|
---|
159 | #ifdef RT_ARCH_AMD64
|
---|
160 | if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC)
|
---|
161 | # ifdef USE_KMEM_ALLOC_PROT
|
---|
162 | kmem_free(kernel_map, (vm_offset_t)pHdr, pHdr->cb);
|
---|
163 | # else
|
---|
164 | vm_map_remove(kernel_map, (vm_offset_t)pHdr, ((vm_offset_t)pHdr) + pHdr->cb);
|
---|
165 | # endif
|
---|
166 | else
|
---|
167 | #endif
|
---|
168 | free(pHdr, M_IPRTHEAP);
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
173 | {
|
---|
174 | void *pv;
|
---|
175 |
|
---|
176 | /*
|
---|
177 | * Validate input.
|
---|
178 | */
|
---|
179 | AssertPtr(pPhys);
|
---|
180 | Assert(cb > 0);
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * This API works in pages, so no need to do any size aligning.
|
---|
184 | */
|
---|
185 | pv = contigmalloc(cb, /* size */
|
---|
186 | M_IPRTCONT, /* type */
|
---|
187 | M_NOWAIT | M_ZERO, /* flags */
|
---|
188 | 0, /* lowest physical address*/
|
---|
189 | _4G-1, /* highest physical address */
|
---|
190 | PAGE_SIZE, /* alignment. */
|
---|
191 | 0); /* boundary */
|
---|
192 | if (pv)
|
---|
193 | {
|
---|
194 | Assert(!((uintptr_t)pv & PAGE_OFFSET_MASK));
|
---|
195 | *pPhys = vtophys(pv);
|
---|
196 | Assert(!(*pPhys & PAGE_OFFSET_MASK));
|
---|
197 | }
|
---|
198 | return pv;
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
203 | {
|
---|
204 | if (pv)
|
---|
205 | {
|
---|
206 | AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
|
---|
207 | contigfree(pv, cb, M_IPRTCONT);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|