1 | /* $Revision: 29027 $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Ring-0 Memory Objects, Common Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_DEFAULT ///@todo RTLOGGROUP_MEM
|
---|
32 | #include <iprt/memobj.h>
|
---|
33 | #include "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/alloc.h>
|
---|
36 | #include <iprt/asm.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/log.h>
|
---|
40 | #include <iprt/mp.h>
|
---|
41 | #include <iprt/param.h>
|
---|
42 | #include <iprt/process.h>
|
---|
43 | #include <iprt/thread.h>
|
---|
44 |
|
---|
45 | #include "internal/memobj.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Internal function for allocating a new memory object.
|
---|
50 | *
|
---|
51 | * @returns The allocated and initialized handle.
|
---|
52 | * @param cbSelf The size of the memory object handle. 0 mean default size.
|
---|
53 | * @param enmType The memory object type.
|
---|
54 | * @param pv The memory object mapping.
|
---|
55 | * @param cb The size of the memory object.
|
---|
56 | */
|
---|
57 | PRTR0MEMOBJINTERNAL rtR0MemObjNew(size_t cbSelf, RTR0MEMOBJTYPE enmType, void *pv, size_t cb)
|
---|
58 | {
|
---|
59 | PRTR0MEMOBJINTERNAL pNew;
|
---|
60 |
|
---|
61 | /* validate the size */
|
---|
62 | if (!cbSelf)
|
---|
63 | cbSelf = sizeof(*pNew);
|
---|
64 | Assert(cbSelf >= sizeof(*pNew));
|
---|
65 | Assert(cbSelf == (uint32_t)cbSelf);
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Allocate and initialize the object.
|
---|
69 | */
|
---|
70 | pNew = (PRTR0MEMOBJINTERNAL)RTMemAllocZ(cbSelf);
|
---|
71 | if (pNew)
|
---|
72 | {
|
---|
73 | pNew->u32Magic = RTR0MEMOBJ_MAGIC;
|
---|
74 | pNew->cbSelf = (uint32_t)cbSelf;
|
---|
75 | pNew->enmType = enmType;
|
---|
76 | pNew->fFlags = 0;
|
---|
77 | pNew->cb = cb;
|
---|
78 | pNew->pv = pv;
|
---|
79 | }
|
---|
80 | return pNew;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Deletes an incomplete memory object.
|
---|
86 | *
|
---|
87 | * This is for cleaning up after failures during object creation.
|
---|
88 | *
|
---|
89 | * @param pMem The incomplete memory object to delete.
|
---|
90 | */
|
---|
91 | void rtR0MemObjDelete(PRTR0MEMOBJINTERNAL pMem)
|
---|
92 | {
|
---|
93 | if (pMem)
|
---|
94 | {
|
---|
95 | ASMAtomicUoWriteU32(&pMem->u32Magic, ~RTR0MEMOBJ_MAGIC);
|
---|
96 | pMem->enmType = RTR0MEMOBJTYPE_END;
|
---|
97 | RTMemFree(pMem);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Links a mapping object to a primary object.
|
---|
104 | *
|
---|
105 | * @returns IPRT status code.
|
---|
106 | * @retval VINF_SUCCESS on success.
|
---|
107 | * @retval VINF_NO_MEMORY if we couldn't expand the mapping array of the parent.
|
---|
108 | * @param pParent The parent (primary) memory object.
|
---|
109 | * @param pChild The child (mapping) memory object.
|
---|
110 | */
|
---|
111 | static int rtR0MemObjLink(PRTR0MEMOBJINTERNAL pParent, PRTR0MEMOBJINTERNAL pChild)
|
---|
112 | {
|
---|
113 | uint32_t i;
|
---|
114 |
|
---|
115 | /* sanity */
|
---|
116 | Assert(rtR0MemObjIsMapping(pChild));
|
---|
117 | Assert(!rtR0MemObjIsMapping(pParent));
|
---|
118 |
|
---|
119 | /* expand the array? */
|
---|
120 | i = pParent->uRel.Parent.cMappings;
|
---|
121 | if (i >= pParent->uRel.Parent.cMappingsAllocated)
|
---|
122 | {
|
---|
123 | void *pv = RTMemRealloc(pParent->uRel.Parent.papMappings,
|
---|
124 | (i + 32) * sizeof(pParent->uRel.Parent.papMappings[0]));
|
---|
125 | if (!pv)
|
---|
126 | return VERR_NO_MEMORY;
|
---|
127 | pParent->uRel.Parent.papMappings = (PPRTR0MEMOBJINTERNAL)pv;
|
---|
128 | pParent->uRel.Parent.cMappingsAllocated = i + 32;
|
---|
129 | Assert(i == pParent->uRel.Parent.cMappings);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* do the linking. */
|
---|
133 | pParent->uRel.Parent.papMappings[i] = pChild;
|
---|
134 | pParent->uRel.Parent.cMappings++;
|
---|
135 | pChild->uRel.Child.pParent = pParent;
|
---|
136 |
|
---|
137 | return VINF_SUCCESS;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Checks if this is mapping or not.
|
---|
143 | *
|
---|
144 | * @returns true if it's a mapping, otherwise false.
|
---|
145 | * @param MemObj The ring-0 memory object handle.
|
---|
146 | */
|
---|
147 | RTR0DECL(bool) RTR0MemObjIsMapping(RTR0MEMOBJ MemObj)
|
---|
148 | {
|
---|
149 | /* Validate the object handle. */
|
---|
150 | PRTR0MEMOBJINTERNAL pMem;
|
---|
151 | AssertPtrReturn(MemObj, false);
|
---|
152 | pMem = (PRTR0MEMOBJINTERNAL)MemObj;
|
---|
153 | AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), false);
|
---|
154 | AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), false);
|
---|
155 |
|
---|
156 | /* hand it on to the inlined worker. */
|
---|
157 | return rtR0MemObjIsMapping(pMem);
|
---|
158 | }
|
---|
159 | RT_EXPORT_SYMBOL(RTR0MemObjIsMapping);
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Gets the address of a ring-0 memory object.
|
---|
164 | *
|
---|
165 | * @returns The address of the memory object.
|
---|
166 | * @returns NULL if the handle is invalid (asserts in strict builds) or if there isn't any mapping.
|
---|
167 | * @param MemObj The ring-0 memory object handle.
|
---|
168 | */
|
---|
169 | RTR0DECL(void *) RTR0MemObjAddress(RTR0MEMOBJ MemObj)
|
---|
170 | {
|
---|
171 | /* Validate the object handle. */
|
---|
172 | PRTR0MEMOBJINTERNAL pMem;
|
---|
173 | if (RT_UNLIKELY(MemObj == NIL_RTR0MEMOBJ))
|
---|
174 | return NULL;
|
---|
175 | AssertPtrReturn(MemObj, NULL);
|
---|
176 | pMem = (PRTR0MEMOBJINTERNAL)MemObj;
|
---|
177 | AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), NULL);
|
---|
178 | AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), NULL);
|
---|
179 |
|
---|
180 | /* return the mapping address. */
|
---|
181 | return pMem->pv;
|
---|
182 | }
|
---|
183 | RT_EXPORT_SYMBOL(RTR0MemObjAddress);
|
---|
184 |
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Gets the ring-3 address of a ring-0 memory object.
|
---|
188 | *
|
---|
189 | * This only applies to ring-0 memory object with ring-3 mappings of some kind, i.e.
|
---|
190 | * locked user memory, reserved user address space and user mappings. This API should
|
---|
191 | * not be used on any other objects.
|
---|
192 | *
|
---|
193 | * @returns The address of the memory object.
|
---|
194 | * @returns NIL_RTR3PTR if the handle is invalid or if it's not an object with a ring-3 mapping.
|
---|
195 | * Strict builds will assert in both cases.
|
---|
196 | * @param MemObj The ring-0 memory object handle.
|
---|
197 | */
|
---|
198 | RTR0DECL(RTR3PTR) RTR0MemObjAddressR3(RTR0MEMOBJ MemObj)
|
---|
199 | {
|
---|
200 | PRTR0MEMOBJINTERNAL pMem;
|
---|
201 |
|
---|
202 | /* Validate the object handle. */
|
---|
203 | if (RT_UNLIKELY(MemObj == NIL_RTR0MEMOBJ))
|
---|
204 | return NIL_RTR3PTR;
|
---|
205 | AssertPtrReturn(MemObj, NIL_RTR3PTR);
|
---|
206 | pMem = (PRTR0MEMOBJINTERNAL)MemObj;
|
---|
207 | AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), NIL_RTR3PTR);
|
---|
208 | AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), NIL_RTR3PTR);
|
---|
209 | if (RT_UNLIKELY( ( pMem->enmType != RTR0MEMOBJTYPE_MAPPING
|
---|
210 | || pMem->u.Mapping.R0Process == NIL_RTR0PROCESS)
|
---|
211 | && ( pMem->enmType != RTR0MEMOBJTYPE_LOCK
|
---|
212 | || pMem->u.Lock.R0Process == NIL_RTR0PROCESS)
|
---|
213 | && ( pMem->enmType != RTR0MEMOBJTYPE_PHYS_NC
|
---|
214 | || pMem->u.Lock.R0Process == NIL_RTR0PROCESS)
|
---|
215 | && ( pMem->enmType != RTR0MEMOBJTYPE_RES_VIRT
|
---|
216 | || pMem->u.ResVirt.R0Process == NIL_RTR0PROCESS)))
|
---|
217 | return NIL_RTR3PTR;
|
---|
218 |
|
---|
219 | /* return the mapping address. */
|
---|
220 | return (RTR3PTR)pMem->pv;
|
---|
221 | }
|
---|
222 | RT_EXPORT_SYMBOL(RTR0MemObjAddressR3);
|
---|
223 |
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Gets the size of a ring-0 memory object.
|
---|
227 | *
|
---|
228 | * @returns The address of the memory object.
|
---|
229 | * @returns 0 if the handle is invalid (asserts in strict builds) or if there isn't any mapping.
|
---|
230 | * @param MemObj The ring-0 memory object handle.
|
---|
231 | */
|
---|
232 | RTR0DECL(size_t) RTR0MemObjSize(RTR0MEMOBJ MemObj)
|
---|
233 | {
|
---|
234 | PRTR0MEMOBJINTERNAL pMem;
|
---|
235 |
|
---|
236 | /* Validate the object handle. */
|
---|
237 | if (RT_UNLIKELY(MemObj == NIL_RTR0MEMOBJ))
|
---|
238 | return 0;
|
---|
239 | AssertPtrReturn(MemObj, 0);
|
---|
240 | pMem = (PRTR0MEMOBJINTERNAL)MemObj;
|
---|
241 | AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), 0);
|
---|
242 | AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), 0);
|
---|
243 |
|
---|
244 | /* return the size. */
|
---|
245 | return pMem->cb;
|
---|
246 | }
|
---|
247 | RT_EXPORT_SYMBOL(RTR0MemObjSize);
|
---|
248 |
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Get the physical address of an page in the memory object.
|
---|
252 | *
|
---|
253 | * @returns The physical address.
|
---|
254 | * @returns NIL_RTHCPHYS if the object doesn't contain fixed physical pages.
|
---|
255 | * @returns NIL_RTHCPHYS if the iPage is out of range.
|
---|
256 | * @returns NIL_RTHCPHYS if the object handle isn't valid.
|
---|
257 | * @param MemObj The ring-0 memory object handle.
|
---|
258 | * @param iPage The page number within the object.
|
---|
259 | */
|
---|
260 | RTR0DECL(RTHCPHYS) RTR0MemObjGetPagePhysAddr(RTR0MEMOBJ MemObj, size_t iPage)
|
---|
261 | {
|
---|
262 | /* Validate the object handle. */
|
---|
263 | PRTR0MEMOBJINTERNAL pMem;
|
---|
264 | size_t cPages;
|
---|
265 | AssertPtrReturn(MemObj, NIL_RTHCPHYS);
|
---|
266 | pMem = (PRTR0MEMOBJINTERNAL)MemObj;
|
---|
267 | AssertReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, NIL_RTHCPHYS);
|
---|
268 | AssertReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, NIL_RTHCPHYS);
|
---|
269 | AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), NIL_RTHCPHYS);
|
---|
270 | AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), NIL_RTHCPHYS);
|
---|
271 | cPages = (pMem->cb >> PAGE_SHIFT);
|
---|
272 | if (iPage >= cPages)
|
---|
273 | {
|
---|
274 | /* permit: while (RTR0MemObjGetPagePhysAddr(pMem, iPage++) != NIL_RTHCPHYS) {} */
|
---|
275 | if (iPage == cPages)
|
---|
276 | return NIL_RTHCPHYS;
|
---|
277 | AssertReturn(iPage < (pMem->cb >> PAGE_SHIFT), NIL_RTHCPHYS);
|
---|
278 | }
|
---|
279 |
|
---|
280 | /*
|
---|
281 | * We know the address of physically contiguous allocations and mappings.
|
---|
282 | */
|
---|
283 | if (pMem->enmType == RTR0MEMOBJTYPE_CONT)
|
---|
284 | return pMem->u.Cont.Phys + iPage * PAGE_SIZE;
|
---|
285 | if (pMem->enmType == RTR0MEMOBJTYPE_PHYS)
|
---|
286 | return pMem->u.Phys.PhysBase + iPage * PAGE_SIZE;
|
---|
287 |
|
---|
288 | /*
|
---|
289 | * Do the job.
|
---|
290 | */
|
---|
291 | return rtR0MemObjNativeGetPagePhysAddr(pMem, iPage);
|
---|
292 | }
|
---|
293 | RT_EXPORT_SYMBOL(RTR0MemObjGetPagePhysAddr);
|
---|
294 |
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Frees a ring-0 memory object.
|
---|
298 | *
|
---|
299 | * @returns IPRT status code.
|
---|
300 | * @retval VERR_INVALID_HANDLE if
|
---|
301 | * @param MemObj The ring-0 memory object to be freed. NULL is accepted.
|
---|
302 | * @param fFreeMappings Whether or not to free mappings of the object.
|
---|
303 | */
|
---|
304 | RTR0DECL(int) RTR0MemObjFree(RTR0MEMOBJ MemObj, bool fFreeMappings)
|
---|
305 | {
|
---|
306 | /*
|
---|
307 | * Validate the object handle.
|
---|
308 | */
|
---|
309 | PRTR0MEMOBJINTERNAL pMem;
|
---|
310 | int rc;
|
---|
311 |
|
---|
312 | if (MemObj == NIL_RTR0MEMOBJ)
|
---|
313 | return VINF_SUCCESS;
|
---|
314 | AssertPtrReturn(MemObj, VERR_INVALID_HANDLE);
|
---|
315 | pMem = (PRTR0MEMOBJINTERNAL)MemObj;
|
---|
316 | AssertReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
|
---|
317 | AssertReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
|
---|
318 | RT_ASSERT_PREEMPTIBLE();
|
---|
319 |
|
---|
320 | /*
|
---|
321 | * Deal with mapings according to fFreeMappings.
|
---|
322 | */
|
---|
323 | if ( !rtR0MemObjIsMapping(pMem)
|
---|
324 | && pMem->uRel.Parent.cMappings > 0)
|
---|
325 | {
|
---|
326 | /* fail if not requested to free mappings. */
|
---|
327 | if (!fFreeMappings)
|
---|
328 | return VERR_MEMORY_BUSY;
|
---|
329 |
|
---|
330 | while (pMem->uRel.Parent.cMappings > 0)
|
---|
331 | {
|
---|
332 | PRTR0MEMOBJINTERNAL pChild = pMem->uRel.Parent.papMappings[--pMem->uRel.Parent.cMappings];
|
---|
333 | pMem->uRel.Parent.papMappings[pMem->uRel.Parent.cMappings] = NULL;
|
---|
334 |
|
---|
335 | /* sanity checks. */
|
---|
336 | AssertPtr(pChild);
|
---|
337 | AssertFatal(pChild->u32Magic == RTR0MEMOBJ_MAGIC);
|
---|
338 | AssertFatal(pChild->enmType > RTR0MEMOBJTYPE_INVALID && pChild->enmType < RTR0MEMOBJTYPE_END);
|
---|
339 | AssertFatal(rtR0MemObjIsMapping(pChild));
|
---|
340 |
|
---|
341 | /* free the mapping. */
|
---|
342 | rc = rtR0MemObjNativeFree(pChild);
|
---|
343 | if (RT_FAILURE(rc))
|
---|
344 | {
|
---|
345 | Log(("RTR0MemObjFree: failed to free mapping %p: %p %#zx; rc=%Rrc\n", pChild, pChild->pv, pChild->cb, rc));
|
---|
346 | pMem->uRel.Parent.papMappings[pMem->uRel.Parent.cMappings++] = pChild;
|
---|
347 | return rc;
|
---|
348 | }
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * Free this object.
|
---|
354 | */
|
---|
355 | rc = rtR0MemObjNativeFree(pMem);
|
---|
356 | if (RT_SUCCESS(rc))
|
---|
357 | {
|
---|
358 | /*
|
---|
359 | * Ok, it was freed just fine. Now, if it's a mapping we'll have to remove it from the parent.
|
---|
360 | */
|
---|
361 | if (rtR0MemObjIsMapping(pMem))
|
---|
362 | {
|
---|
363 | PRTR0MEMOBJINTERNAL pParent = pMem->uRel.Child.pParent;
|
---|
364 | uint32_t i;
|
---|
365 |
|
---|
366 | /* sanity checks */
|
---|
367 | AssertPtr(pParent);
|
---|
368 | AssertFatal(pParent->u32Magic == RTR0MEMOBJ_MAGIC);
|
---|
369 | AssertFatal(pParent->enmType > RTR0MEMOBJTYPE_INVALID && pParent->enmType < RTR0MEMOBJTYPE_END);
|
---|
370 | AssertFatal(!rtR0MemObjIsMapping(pParent));
|
---|
371 | AssertFatal(pParent->uRel.Parent.cMappings > 0);
|
---|
372 | AssertPtr(pParent->uRel.Parent.papMappings);
|
---|
373 |
|
---|
374 | /* locate and remove from the array of mappings. */
|
---|
375 | i = pParent->uRel.Parent.cMappings;
|
---|
376 | while (i-- > 0)
|
---|
377 | {
|
---|
378 | if (pParent->uRel.Parent.papMappings[i] == pMem)
|
---|
379 | {
|
---|
380 | pParent->uRel.Parent.papMappings[i] = pParent->uRel.Parent.papMappings[--pParent->uRel.Parent.cMappings];
|
---|
381 | break;
|
---|
382 | }
|
---|
383 | }
|
---|
384 | Assert(i != UINT32_MAX);
|
---|
385 | }
|
---|
386 | else
|
---|
387 | Assert(pMem->uRel.Parent.cMappings == 0);
|
---|
388 |
|
---|
389 | /*
|
---|
390 | * Finally, destroy the handle.
|
---|
391 | */
|
---|
392 | pMem->u32Magic++;
|
---|
393 | pMem->enmType = RTR0MEMOBJTYPE_END;
|
---|
394 | if (!rtR0MemObjIsMapping(pMem))
|
---|
395 | RTMemFree(pMem->uRel.Parent.papMappings);
|
---|
396 | RTMemFree(pMem);
|
---|
397 | }
|
---|
398 | else
|
---|
399 | Log(("RTR0MemObjFree: failed to free %p: %d %p %#zx; rc=%Rrc\n",
|
---|
400 | pMem, pMem->enmType, pMem->pv, pMem->cb, rc));
|
---|
401 | return rc;
|
---|
402 | }
|
---|
403 | RT_EXPORT_SYMBOL(RTR0MemObjFree);
|
---|
404 |
|
---|
405 |
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Allocates page aligned virtual kernel memory.
|
---|
409 | *
|
---|
410 | * The memory is taken from a non paged (= fixed physical memory backing) pool.
|
---|
411 | *
|
---|
412 | * @returns IPRT status code.
|
---|
413 | * @param pMemObj Where to store the ring-0 memory object handle.
|
---|
414 | * @param cb Number of bytes to allocate. This is rounded up to nearest page.
|
---|
415 | * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
|
---|
416 | */
|
---|
417 | RTR0DECL(int) RTR0MemObjAllocPage(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable)
|
---|
418 | {
|
---|
419 | /* sanity checks. */
|
---|
420 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
421 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
422 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
423 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
424 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
425 | RT_ASSERT_PREEMPTIBLE();
|
---|
426 |
|
---|
427 | /* do the allocation. */
|
---|
428 | return rtR0MemObjNativeAllocPage(pMemObj, cbAligned, fExecutable);
|
---|
429 | }
|
---|
430 | RT_EXPORT_SYMBOL(RTR0MemObjAllocPage);
|
---|
431 |
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * Allocates page aligned virtual kernel memory with physical backing below 4GB.
|
---|
435 | *
|
---|
436 | * The physical memory backing the allocation is fixed.
|
---|
437 | *
|
---|
438 | * @returns IPRT status code.
|
---|
439 | * @param pMemObj Where to store the ring-0 memory object handle.
|
---|
440 | * @param cb Number of bytes to allocate. This is rounded up to nearest page.
|
---|
441 | * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
|
---|
442 | */
|
---|
443 | RTR0DECL(int) RTR0MemObjAllocLow(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable)
|
---|
444 | {
|
---|
445 | /* sanity checks. */
|
---|
446 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
447 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
448 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
449 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
450 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
451 | RT_ASSERT_PREEMPTIBLE();
|
---|
452 |
|
---|
453 | /* do the allocation. */
|
---|
454 | return rtR0MemObjNativeAllocLow(pMemObj, cbAligned, fExecutable);
|
---|
455 | }
|
---|
456 | RT_EXPORT_SYMBOL(RTR0MemObjAllocLow);
|
---|
457 |
|
---|
458 |
|
---|
459 | /**
|
---|
460 | * Allocates page aligned virtual kernel memory with contiguous physical backing below 4GB.
|
---|
461 | *
|
---|
462 | * The physical memory backing the allocation is fixed.
|
---|
463 | *
|
---|
464 | * @returns IPRT status code.
|
---|
465 | * @param pMemObj Where to store the ring-0 memory object handle.
|
---|
466 | * @param cb Number of bytes to allocate. This is rounded up to nearest page.
|
---|
467 | * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
|
---|
468 | */
|
---|
469 | RTR0DECL(int) RTR0MemObjAllocCont(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable)
|
---|
470 | {
|
---|
471 | /* sanity checks. */
|
---|
472 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
473 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
474 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
475 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
476 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
477 | RT_ASSERT_PREEMPTIBLE();
|
---|
478 |
|
---|
479 | /* do the allocation. */
|
---|
480 | return rtR0MemObjNativeAllocCont(pMemObj, cbAligned, fExecutable);
|
---|
481 | }
|
---|
482 | RT_EXPORT_SYMBOL(RTR0MemObjAllocCont);
|
---|
483 |
|
---|
484 |
|
---|
485 | /**
|
---|
486 | * Locks a range of user virtual memory.
|
---|
487 | *
|
---|
488 | * @returns IPRT status code.
|
---|
489 | * @param pMemObj Where to store the ring-0 memory object handle.
|
---|
490 | * @param R3Ptr User virtual address. This is rounded down to a page
|
---|
491 | * boundrary.
|
---|
492 | * @param cb Number of bytes to lock. This is rounded up to
|
---|
493 | * nearest page boundrary.
|
---|
494 | * @param fAccess The desired access, a combination of RTMEM_PROT_READ
|
---|
495 | * and RTMEM_PROT_WRITE.
|
---|
496 | * @param R0Process The process to lock pages in. NIL_RTR0PROCESS is an
|
---|
497 | * alias for the current one.
|
---|
498 | *
|
---|
499 | * @remarks RTR0MemGetAddressR3() and RTR0MemGetAddress() will return therounded
|
---|
500 | * down address.
|
---|
501 | *
|
---|
502 | * @remarks Linux: This API requires that the memory begin locked is in a memory
|
---|
503 | * mapping that is not required in any forked off child process. This
|
---|
504 | * is not intented as permanent restriction, feel free to help out
|
---|
505 | * lifting it.
|
---|
506 | */
|
---|
507 | RTR0DECL(int) RTR0MemObjLockUser(PRTR0MEMOBJ pMemObj, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
|
---|
508 | {
|
---|
509 | /* sanity checks. */
|
---|
510 | const size_t cbAligned = RT_ALIGN_Z(cb + (R3Ptr & PAGE_OFFSET_MASK), PAGE_SIZE);
|
---|
511 | RTR3PTR const R3PtrAligned = (R3Ptr & ~(RTR3PTR)PAGE_OFFSET_MASK);
|
---|
512 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
513 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
514 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
515 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
516 | if (R0Process == NIL_RTR0PROCESS)
|
---|
517 | R0Process = RTR0ProcHandleSelf();
|
---|
518 | AssertReturn(!(fAccess & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE)), VERR_INVALID_PARAMETER);
|
---|
519 | AssertReturn(fAccess, VERR_INVALID_PARAMETER);
|
---|
520 | RT_ASSERT_PREEMPTIBLE();
|
---|
521 |
|
---|
522 | /* do the locking. */
|
---|
523 | return rtR0MemObjNativeLockUser(pMemObj, R3PtrAligned, cbAligned, fAccess, R0Process);
|
---|
524 | }
|
---|
525 | RT_EXPORT_SYMBOL(RTR0MemObjLockUser);
|
---|
526 |
|
---|
527 |
|
---|
528 | /**
|
---|
529 | * Locks a range of kernel virtual memory.
|
---|
530 | *
|
---|
531 | * @returns IPRT status code.
|
---|
532 | * @param pMemObj Where to store the ring-0 memory object handle.
|
---|
533 | * @param pv Kernel virtual address. This is rounded down to a page boundrary.
|
---|
534 | * @param cb Number of bytes to lock. This is rounded up to nearest page boundrary.
|
---|
535 | * @param fAccess The desired access, a combination of RTMEM_PROT_READ
|
---|
536 | * and RTMEM_PROT_WRITE.
|
---|
537 | *
|
---|
538 | * @remark RTR0MemGetAddress() will return the rounded down address.
|
---|
539 | */
|
---|
540 | RTR0DECL(int) RTR0MemObjLockKernel(PRTR0MEMOBJ pMemObj, void *pv, size_t cb, uint32_t fAccess)
|
---|
541 | {
|
---|
542 | /* sanity checks. */
|
---|
543 | const size_t cbAligned = RT_ALIGN_Z(cb + ((uintptr_t)pv & PAGE_OFFSET_MASK), PAGE_SIZE);
|
---|
544 | void * const pvAligned = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
|
---|
545 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
546 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
547 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
548 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
549 | AssertPtrReturn(pvAligned, VERR_INVALID_POINTER);
|
---|
550 | AssertReturn(!(fAccess & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE)), VERR_INVALID_PARAMETER);
|
---|
551 | AssertReturn(fAccess, VERR_INVALID_PARAMETER);
|
---|
552 | RT_ASSERT_PREEMPTIBLE();
|
---|
553 |
|
---|
554 | /* do the allocation. */
|
---|
555 | return rtR0MemObjNativeLockKernel(pMemObj, pvAligned, cbAligned, fAccess);
|
---|
556 | }
|
---|
557 | RT_EXPORT_SYMBOL(RTR0MemObjLockKernel);
|
---|
558 |
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * Allocates contiguous page aligned physical memory without (necessarily) any kernel mapping.
|
---|
562 | *
|
---|
563 | * @returns IPRT status code.
|
---|
564 | * @param pMemObj Where to store the ring-0 memory object handle.
|
---|
565 | * @param cb Number of bytes to allocate. This is rounded up to nearest page.
|
---|
566 | * @param PhysHighest The highest permittable address (inclusive).
|
---|
567 | * Pass NIL_RTHCPHYS if any address is acceptable.
|
---|
568 | */
|
---|
569 | RTR0DECL(int) RTR0MemObjAllocPhys(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest)
|
---|
570 | {
|
---|
571 | /* sanity checks. */
|
---|
572 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
573 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
574 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
575 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
576 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
577 | AssertReturn(PhysHighest >= cb, VERR_INVALID_PARAMETER);
|
---|
578 | RT_ASSERT_PREEMPTIBLE();
|
---|
579 |
|
---|
580 | /* do the allocation. */
|
---|
581 | return rtR0MemObjNativeAllocPhys(pMemObj, cbAligned, PhysHighest, PAGE_SIZE /* page aligned */);
|
---|
582 | }
|
---|
583 | RT_EXPORT_SYMBOL(RTR0MemObjAllocPhys);
|
---|
584 |
|
---|
585 |
|
---|
586 | /**
|
---|
587 | * Allocates contiguous physical memory without (necessarily) any kernel mapping.
|
---|
588 | *
|
---|
589 | * @returns IPRT status code.
|
---|
590 | * @param pMemObj Where to store the ring-0 memory object handle.
|
---|
591 | * @param cb Number of bytes to allocate. This is rounded up to nearest page.
|
---|
592 | * @param PhysHighest The highest permittable address (inclusive).
|
---|
593 | * Pass NIL_RTHCPHYS if any address is acceptable.
|
---|
594 | * @param uAlignment The alignment of the physical memory to allocate.
|
---|
595 | * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M, _4M and _1G.
|
---|
596 | */
|
---|
597 | RTR0DECL(int) RTR0MemObjAllocPhysEx(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
|
---|
598 | {
|
---|
599 | /* sanity checks. */
|
---|
600 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
601 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
602 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
603 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
604 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
605 | AssertReturn(PhysHighest >= cb, VERR_INVALID_PARAMETER);
|
---|
606 | if (uAlignment == 0)
|
---|
607 | uAlignment = PAGE_SIZE;
|
---|
608 | AssertReturn( uAlignment == PAGE_SIZE
|
---|
609 | || uAlignment == _2M
|
---|
610 | || uAlignment == _4M
|
---|
611 | || uAlignment == _1G,
|
---|
612 | VERR_INVALID_PARAMETER);
|
---|
613 | #if HC_ARCH_BITS == 32
|
---|
614 | /* Memory allocated in this way is typically mapped into kernel space as well; simply
|
---|
615 | don't allow this on 32 bits hosts as the kernel space is too crowded already. */
|
---|
616 | if (uAlignment != PAGE_SIZE)
|
---|
617 | return VERR_NOT_SUPPORTED;
|
---|
618 | #endif
|
---|
619 | RT_ASSERT_PREEMPTIBLE();
|
---|
620 |
|
---|
621 | /* do the allocation. */
|
---|
622 | return rtR0MemObjNativeAllocPhys(pMemObj, cbAligned, PhysHighest, uAlignment);
|
---|
623 | }
|
---|
624 | RT_EXPORT_SYMBOL(RTR0MemObjAllocPhysEx);
|
---|
625 |
|
---|
626 |
|
---|
627 | /**
|
---|
628 | * Allocates non-contiguous page aligned physical memory without (necessarily) any kernel mapping.
|
---|
629 | *
|
---|
630 | * @returns IPRT status code.
|
---|
631 | * @param pMemObj Where to store the ring-0 memory object handle.
|
---|
632 | * @param cb Number of bytes to allocate. This is rounded up to nearest page.
|
---|
633 | * @param PhysHighest The highest permittable address (inclusive).
|
---|
634 | * Pass NIL_RTHCPHYS if any address is acceptable.
|
---|
635 | */
|
---|
636 | RTR0DECL(int) RTR0MemObjAllocPhysNC(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest)
|
---|
637 | {
|
---|
638 | /* sanity checks. */
|
---|
639 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
640 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
641 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
642 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
643 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
644 | AssertReturn(PhysHighest >= cb, VERR_INVALID_PARAMETER);
|
---|
645 | RT_ASSERT_PREEMPTIBLE();
|
---|
646 |
|
---|
647 | /* do the allocation. */
|
---|
648 | return rtR0MemObjNativeAllocPhysNC(pMemObj, cbAligned, PhysHighest);
|
---|
649 | }
|
---|
650 | RT_EXPORT_SYMBOL(RTR0MemObjAllocPhysNC);
|
---|
651 |
|
---|
652 |
|
---|
653 | /**
|
---|
654 | * Creates a page aligned, contiguous, physical memory object.
|
---|
655 | *
|
---|
656 | * No physical memory is allocated, we trust you do know what you're doing.
|
---|
657 | *
|
---|
658 | * @returns IPRT status code.
|
---|
659 | * @param pMemObj Where to store the ring-0 memory object handle.
|
---|
660 | * @param Phys The physical address to start at. This is rounded down to the
|
---|
661 | * nearest page boundrary.
|
---|
662 | * @param cb The size of the object in bytes. This is rounded up to nearest page boundrary.
|
---|
663 | * @param uCachePolicy One of the RTMEM_CACHE_XXX modes.
|
---|
664 | */
|
---|
665 | RTR0DECL(int) RTR0MemObjEnterPhys(PRTR0MEMOBJ pMemObj, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
|
---|
666 | {
|
---|
667 | /* sanity checks. */
|
---|
668 | const size_t cbAligned = RT_ALIGN_Z(cb + (Phys & PAGE_OFFSET_MASK), PAGE_SIZE);
|
---|
669 | const RTHCPHYS PhysAligned = Phys & ~(RTHCPHYS)PAGE_OFFSET_MASK;
|
---|
670 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
671 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
672 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
673 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
674 | AssertReturn(Phys != NIL_RTHCPHYS, VERR_INVALID_PARAMETER);
|
---|
675 | AssertReturn( uCachePolicy == RTMEM_CACHE_POLICY_DONT_CARE
|
---|
676 | || uCachePolicy == RTMEM_CACHE_POLICY_MMIO,
|
---|
677 | VERR_INVALID_PARAMETER);
|
---|
678 | RT_ASSERT_PREEMPTIBLE();
|
---|
679 |
|
---|
680 | /* do the allocation. */
|
---|
681 | return rtR0MemObjNativeEnterPhys(pMemObj, PhysAligned, cbAligned, uCachePolicy);
|
---|
682 | }
|
---|
683 | RT_EXPORT_SYMBOL(RTR0MemObjEnterPhys);
|
---|
684 |
|
---|
685 |
|
---|
686 | /**
|
---|
687 | * Reserves kernel virtual address space.
|
---|
688 | *
|
---|
689 | * @returns IPRT status code.
|
---|
690 | * @param pMemObj Where to store the ring-0 memory object handle.
|
---|
691 | * @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
|
---|
692 | * @param cb The number of bytes to reserve. This is rounded up to nearest page.
|
---|
693 | * @param uAlignment The alignment of the reserved memory.
|
---|
694 | * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
|
---|
695 | */
|
---|
696 | RTR0DECL(int) RTR0MemObjReserveKernel(PRTR0MEMOBJ pMemObj, void *pvFixed, size_t cb, size_t uAlignment)
|
---|
697 | {
|
---|
698 | /* sanity checks. */
|
---|
699 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
700 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
701 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
702 | if (uAlignment == 0)
|
---|
703 | uAlignment = PAGE_SIZE;
|
---|
704 | AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
|
---|
705 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
706 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
707 | if (pvFixed != (void *)-1)
|
---|
708 | AssertReturn(!((uintptr_t)pvFixed & (uAlignment - 1)), VERR_INVALID_PARAMETER);
|
---|
709 | RT_ASSERT_PREEMPTIBLE();
|
---|
710 |
|
---|
711 | /* do the reservation. */
|
---|
712 | return rtR0MemObjNativeReserveKernel(pMemObj, pvFixed, cbAligned, uAlignment);
|
---|
713 | }
|
---|
714 | RT_EXPORT_SYMBOL(RTR0MemObjReserveKernel);
|
---|
715 |
|
---|
716 |
|
---|
717 | /**
|
---|
718 | * Reserves user virtual address space in the current process.
|
---|
719 | *
|
---|
720 | * @returns IPRT status code.
|
---|
721 | * @param pMemObj Where to store the ring-0 memory object handle.
|
---|
722 | * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This must match the alignment.
|
---|
723 | * @param cb The number of bytes to reserve. This is rounded up to nearest PAGE_SIZE.
|
---|
724 | * @param uAlignment The alignment of the reserved memory.
|
---|
725 | * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
|
---|
726 | * @param R0Process The process to reserve the memory in. NIL_RTR0PROCESS is an alias for the current one.
|
---|
727 | */
|
---|
728 | RTR0DECL(int) RTR0MemObjReserveUser(PRTR0MEMOBJ pMemObj, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
|
---|
729 | {
|
---|
730 | /* sanity checks. */
|
---|
731 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
732 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
733 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
734 | if (uAlignment == 0)
|
---|
735 | uAlignment = PAGE_SIZE;
|
---|
736 | AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
|
---|
737 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
738 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
739 | if (R3PtrFixed != (RTR3PTR)-1)
|
---|
740 | AssertReturn(!(R3PtrFixed & (uAlignment - 1)), VERR_INVALID_PARAMETER);
|
---|
741 | if (R0Process == NIL_RTR0PROCESS)
|
---|
742 | R0Process = RTR0ProcHandleSelf();
|
---|
743 | RT_ASSERT_PREEMPTIBLE();
|
---|
744 |
|
---|
745 | /* do the reservation. */
|
---|
746 | return rtR0MemObjNativeReserveUser(pMemObj, R3PtrFixed, cbAligned, uAlignment, R0Process);
|
---|
747 | }
|
---|
748 | RT_EXPORT_SYMBOL(RTR0MemObjReserveUser);
|
---|
749 |
|
---|
750 |
|
---|
751 | /**
|
---|
752 | * Maps a memory object into kernel virtual address space.
|
---|
753 | *
|
---|
754 | * @returns IPRT status code.
|
---|
755 | * @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
|
---|
756 | * @param MemObjToMap The object to be map.
|
---|
757 | * @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
|
---|
758 | * @param uAlignment The alignment of the reserved memory.
|
---|
759 | * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
|
---|
760 | * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
|
---|
761 | */
|
---|
762 | RTR0DECL(int) RTR0MemObjMapKernel(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment, unsigned fProt)
|
---|
763 | {
|
---|
764 | return RTR0MemObjMapKernelEx(pMemObj, MemObjToMap, pvFixed, uAlignment, fProt, 0, 0);
|
---|
765 | }
|
---|
766 | RT_EXPORT_SYMBOL(RTR0MemObjMapKernel);
|
---|
767 |
|
---|
768 |
|
---|
769 | /**
|
---|
770 | * Maps a memory object into kernel virtual address space.
|
---|
771 | *
|
---|
772 | * The ability to map subsections of the object into kernel space is currently
|
---|
773 | * not implemented on all platforms. All/Most of platforms supports mapping the
|
---|
774 | * whole object into kernel space.
|
---|
775 | *
|
---|
776 | * @returns IPRT status code.
|
---|
777 | * @retval VERR_NOT_SUPPORTED if it's not possible to map a subsection of a
|
---|
778 | * memory object on this platform. When you hit this, try implement it.
|
---|
779 | *
|
---|
780 | * @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
|
---|
781 | * @param MemObjToMap The object to be map.
|
---|
782 | * @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
|
---|
783 | * @param uAlignment The alignment of the reserved memory.
|
---|
784 | * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
|
---|
785 | * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
|
---|
786 | * @param offSub Where in the object to start mapping. If non-zero
|
---|
787 | * the value must be page aligned and cbSub must be
|
---|
788 | * non-zero as well.
|
---|
789 | * @param cbSub The size of the part of the object to be mapped. If
|
---|
790 | * zero the entire object is mapped. The value must be
|
---|
791 | * page aligned.
|
---|
792 | */
|
---|
793 | RTR0DECL(int) RTR0MemObjMapKernelEx(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment,
|
---|
794 | unsigned fProt, size_t offSub, size_t cbSub)
|
---|
795 | {
|
---|
796 | PRTR0MEMOBJINTERNAL pMemToMap;
|
---|
797 | PRTR0MEMOBJINTERNAL pNew;
|
---|
798 | int rc;
|
---|
799 |
|
---|
800 | /* sanity checks. */
|
---|
801 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
802 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
803 | AssertPtrReturn(MemObjToMap, VERR_INVALID_HANDLE);
|
---|
804 | pMemToMap = (PRTR0MEMOBJINTERNAL)MemObjToMap;
|
---|
805 | AssertReturn(pMemToMap->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
|
---|
806 | AssertReturn(pMemToMap->enmType > RTR0MEMOBJTYPE_INVALID && pMemToMap->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
|
---|
807 | AssertReturn(!rtR0MemObjIsMapping(pMemToMap), VERR_INVALID_PARAMETER);
|
---|
808 | AssertReturn(pMemToMap->enmType != RTR0MEMOBJTYPE_RES_VIRT, VERR_INVALID_PARAMETER);
|
---|
809 | if (uAlignment == 0)
|
---|
810 | uAlignment = PAGE_SIZE;
|
---|
811 | AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
|
---|
812 | if (pvFixed != (void *)-1)
|
---|
813 | AssertReturn(!((uintptr_t)pvFixed & (uAlignment - 1)), VERR_INVALID_PARAMETER);
|
---|
814 | AssertReturn(fProt != RTMEM_PROT_NONE, VERR_INVALID_PARAMETER);
|
---|
815 | AssertReturn(!(fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
|
---|
816 | AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
817 | AssertReturn(offSub < pMemToMap->cb, VERR_INVALID_PARAMETER);
|
---|
818 | AssertReturn(!(cbSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
819 | AssertReturn(cbSub <= pMemToMap->cb, VERR_INVALID_PARAMETER);
|
---|
820 | AssertReturn((!offSub && !cbSub) || (offSub + cbSub) <= pMemToMap->cb, VERR_INVALID_PARAMETER);
|
---|
821 | RT_ASSERT_PREEMPTIBLE();
|
---|
822 |
|
---|
823 | /* adjust the request to simplify the native code. */
|
---|
824 | if (offSub == 0 && cbSub == pMemToMap->cb)
|
---|
825 | cbSub = 0;
|
---|
826 |
|
---|
827 | /* do the mapping. */
|
---|
828 | rc = rtR0MemObjNativeMapKernel(&pNew, pMemToMap, pvFixed, uAlignment, fProt, offSub, cbSub);
|
---|
829 | if (RT_SUCCESS(rc))
|
---|
830 | {
|
---|
831 | /* link it. */
|
---|
832 | rc = rtR0MemObjLink(pMemToMap, pNew);
|
---|
833 | if (RT_SUCCESS(rc))
|
---|
834 | *pMemObj = pNew;
|
---|
835 | else
|
---|
836 | {
|
---|
837 | /* damn, out of memory. bail out. */
|
---|
838 | int rc2 = rtR0MemObjNativeFree(pNew);
|
---|
839 | AssertRC(rc2);
|
---|
840 | pNew->u32Magic++;
|
---|
841 | pNew->enmType = RTR0MEMOBJTYPE_END;
|
---|
842 | RTMemFree(pNew);
|
---|
843 | }
|
---|
844 | }
|
---|
845 |
|
---|
846 | return rc;
|
---|
847 | }
|
---|
848 | RT_EXPORT_SYMBOL(RTR0MemObjMapKernelEx);
|
---|
849 |
|
---|
850 |
|
---|
851 | /**
|
---|
852 | * Maps a memory object into user virtual address space in the current process.
|
---|
853 | *
|
---|
854 | * @returns IPRT status code.
|
---|
855 | * @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
|
---|
856 | * @param MemObjToMap The object to be map.
|
---|
857 | * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This must match the alignment.
|
---|
858 | * @param uAlignment The alignment of the reserved memory.
|
---|
859 | * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
|
---|
860 | * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
|
---|
861 | * @param R0Process The process to map the memory into. NIL_RTR0PROCESS is an alias for the current one.
|
---|
862 | */
|
---|
863 | RTR0DECL(int) RTR0MemObjMapUser(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
|
---|
864 | {
|
---|
865 | /* sanity checks. */
|
---|
866 | PRTR0MEMOBJINTERNAL pMemToMap;
|
---|
867 | PRTR0MEMOBJINTERNAL pNew;
|
---|
868 | int rc;
|
---|
869 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
870 | pMemToMap = (PRTR0MEMOBJINTERNAL)MemObjToMap;
|
---|
871 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
872 | AssertPtrReturn(MemObjToMap, VERR_INVALID_HANDLE);
|
---|
873 | AssertReturn(pMemToMap->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
|
---|
874 | AssertReturn(pMemToMap->enmType > RTR0MEMOBJTYPE_INVALID && pMemToMap->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
|
---|
875 | AssertReturn(!rtR0MemObjIsMapping(pMemToMap), VERR_INVALID_PARAMETER);
|
---|
876 | AssertReturn(pMemToMap->enmType != RTR0MEMOBJTYPE_RES_VIRT, VERR_INVALID_PARAMETER);
|
---|
877 | if (uAlignment == 0)
|
---|
878 | uAlignment = PAGE_SIZE;
|
---|
879 | AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
|
---|
880 | if (R3PtrFixed != (RTR3PTR)-1)
|
---|
881 | AssertReturn(!(R3PtrFixed & (uAlignment - 1)), VERR_INVALID_PARAMETER);
|
---|
882 | AssertReturn(fProt != RTMEM_PROT_NONE, VERR_INVALID_PARAMETER);
|
---|
883 | AssertReturn(!(fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
|
---|
884 | if (R0Process == NIL_RTR0PROCESS)
|
---|
885 | R0Process = RTR0ProcHandleSelf();
|
---|
886 | RT_ASSERT_PREEMPTIBLE();
|
---|
887 |
|
---|
888 | /* do the mapping. */
|
---|
889 | rc = rtR0MemObjNativeMapUser(&pNew, pMemToMap, R3PtrFixed, uAlignment, fProt, R0Process);
|
---|
890 | if (RT_SUCCESS(rc))
|
---|
891 | {
|
---|
892 | /* link it. */
|
---|
893 | rc = rtR0MemObjLink(pMemToMap, pNew);
|
---|
894 | if (RT_SUCCESS(rc))
|
---|
895 | *pMemObj = pNew;
|
---|
896 | else
|
---|
897 | {
|
---|
898 | /* damn, out of memory. bail out. */
|
---|
899 | int rc2 = rtR0MemObjNativeFree(pNew);
|
---|
900 | AssertRC(rc2);
|
---|
901 | pNew->u32Magic++;
|
---|
902 | pNew->enmType = RTR0MEMOBJTYPE_END;
|
---|
903 | RTMemFree(pNew);
|
---|
904 | }
|
---|
905 | }
|
---|
906 |
|
---|
907 | return rc;
|
---|
908 | }
|
---|
909 | RT_EXPORT_SYMBOL(RTR0MemObjMapUser);
|
---|
910 |
|
---|
911 |
|
---|
912 | RTR0DECL(int) RTR0MemObjProtect(RTR0MEMOBJ hMemObj, size_t offSub, size_t cbSub, uint32_t fProt)
|
---|
913 | {
|
---|
914 | PRTR0MEMOBJINTERNAL pMemObj;
|
---|
915 | int rc;
|
---|
916 |
|
---|
917 | /* sanity checks. */
|
---|
918 | pMemObj = (PRTR0MEMOBJINTERNAL)hMemObj;
|
---|
919 | AssertPtrReturn(pMemObj, VERR_INVALID_HANDLE);
|
---|
920 | AssertReturn(pMemObj->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
|
---|
921 | AssertReturn(pMemObj->enmType > RTR0MEMOBJTYPE_INVALID && pMemObj->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
|
---|
922 | AssertReturn(rtR0MemObjIsProtectable(pMemObj), VERR_INVALID_PARAMETER);
|
---|
923 | AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
924 | AssertReturn(offSub < pMemObj->cb, VERR_INVALID_PARAMETER);
|
---|
925 | AssertReturn(!(cbSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
926 | AssertReturn(cbSub <= pMemObj->cb, VERR_INVALID_PARAMETER);
|
---|
927 | AssertReturn(offSub + cbSub <= pMemObj->cb, VERR_INVALID_PARAMETER);
|
---|
928 | AssertReturn(!(fProt & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
|
---|
929 | RT_ASSERT_PREEMPTIBLE();
|
---|
930 |
|
---|
931 | /* do the job */
|
---|
932 | rc = rtR0MemObjNativeProtect(pMemObj, offSub, cbSub, fProt);
|
---|
933 | if (RT_SUCCESS(rc))
|
---|
934 | pMemObj->fFlags |= RTR0MEMOBJ_FLAGS_PROT_CHANGED; /* record it */
|
---|
935 |
|
---|
936 | return rc;
|
---|
937 | }
|
---|
938 | RT_EXPORT_SYMBOL(RTR0MemObjProtect);
|
---|
939 |
|
---|