VirtualBox

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

Last change on this file since 86669 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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