VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/memobj-r0drv-freebsd.c@ 18972

Last change on this file since 18972 was 18972, checked in by vboxsync, 16 years ago

IPRT/r0drv/freebsd: properties.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.1 KB
Line 
1/* $Id: memobj-r0drv-freebsd.c 18972 2009-04-16 23:43:08Z vboxsync $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects, FreeBSD.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
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
37#include <iprt/memobj.h>
38#include <iprt/mem.h>
39#include <iprt/err.h>
40#include <iprt/assert.h>
41#include <iprt/log.h>
42#include <iprt/param.h>
43#include <iprt/process.h>
44#include "internal/memobj.h"
45
46
47/*******************************************************************************
48* Structures and Typedefs *
49*******************************************************************************/
50/**
51 * The FreeBSD version of the memory object structure.
52 */
53typedef struct RTR0MEMOBJFREEBSD
54{
55 /** The core structure. */
56 RTR0MEMOBJINTERNAL Core;
57 /** The VM object associated with the allocation. */
58 vm_object_t pObject;
59 /** the VM object associated with the mapping.
60 * In mapping mem object, this is the shadow object?
61 * In a allocation/enter mem object, this is the shared object we constructed (contig, perhaps alloc). */
62 vm_object_t pMappingObject;
63} RTR0MEMOBJFREEBSD, *PRTR0MEMOBJFREEBSD;
64
65
66MALLOC_DEFINE(M_IPRTMOBJ, "iprtmobj", "IPRT - R0MemObj");
67
68/*******************************************************************************
69* Internal Functions *
70*******************************************************************************/
71
72
73int rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
74{
75 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)pMem;
76 int rc;
77
78 switch (pMemFreeBSD->Core.enmType)
79 {
80 case RTR0MEMOBJTYPE_CONT:
81 contigfree(pMemFreeBSD->Core.pv, pMemFreeBSD->Core.cb, M_IPRTMOBJ);
82 if (pMemFreeBSD->pMappingObject)
83 {
84 rc = vm_map_remove(kernel_map,
85 (vm_offset_t)pMemFreeBSD->Core.pv,
86 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
87 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
88 }
89 break;
90
91 case RTR0MEMOBJTYPE_PAGE:
92 if (pMemFreeBSD->pObject)
93 {
94 rc = vm_map_remove(kernel_map,
95 (vm_offset_t)pMemFreeBSD->Core.pv,
96 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
97 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
98 }
99 else
100 {
101 free(pMemFreeBSD->Core.pv, M_IPRTMOBJ);
102 if (pMemFreeBSD->pMappingObject)
103 {
104 rc = vm_map_remove(kernel_map,
105 (vm_offset_t)pMemFreeBSD->Core.pv,
106 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
107 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
108 }
109 }
110 break;
111
112 case RTR0MEMOBJTYPE_LOCK:
113 {
114 int fFlags = VM_MAP_WIRE_NOHOLES;
115 vm_map_t pMap = kernel_map;
116
117 if (pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
118 {
119 pMap = &((struct proc *)pMemFreeBSD->Core.u.Lock.R0Process)->p_vmspace->vm_map;
120 fFlags |= VM_MAP_WIRE_USER;
121 }
122 else
123 fFlags |= VM_MAP_WIRE_SYSTEM;
124
125 rc = vm_map_unwire(pMap,
126 (vm_offset_t)pMemFreeBSD->Core.pv,
127 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb,
128 fFlags);
129 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
130 break;
131 }
132
133 case RTR0MEMOBJTYPE_RES_VIRT:
134 {
135 vm_map_t pMap = kernel_map;
136 if (pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
137 pMap = &((struct proc *)pMemFreeBSD->Core.u.Lock.R0Process)->p_vmspace->vm_map;
138 rc = vm_map_remove(pMap,
139 (vm_offset_t)pMemFreeBSD->Core.pv,
140 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
141 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
142 break;
143 }
144
145 case RTR0MEMOBJTYPE_MAPPING:
146 {
147 vm_map_t pMap = kernel_map;
148
149 /* vm_map_remove will unmap the pages we inserted with pmap_enter */
150 AssertMsg(pMemFreeBSD->pMappingObject != NULL, ("MappingObject is NULL\n"));
151 if (pMemFreeBSD->Core.u.Mapping.R0Process != NIL_RTR0PROCESS)
152 pMap = &((struct proc *)pMemFreeBSD->Core.u.Mapping.R0Process)->p_vmspace->vm_map;
153
154 rc = vm_map_remove(pMap,
155 (vm_offset_t)pMemFreeBSD->Core.pv,
156 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
157 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
158 break;
159 }
160
161 /* unused: */
162 case RTR0MEMOBJTYPE_LOW:
163 case RTR0MEMOBJTYPE_PHYS:
164 case RTR0MEMOBJTYPE_PHYS_NC:
165 default:
166 AssertMsgFailed(("enmType=%d\n", pMemFreeBSD->Core.enmType));
167 return VERR_INTERNAL_ERROR;
168 }
169
170 return VINF_SUCCESS;
171}
172
173
174int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
175{
176 int rc;
177
178 /* create the object. */
179 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_PAGE, NULL, cb);
180 if (!pMemFreeBSD)
181 return VERR_NO_MEMORY;
182
183 /*
184 * We've two options here both expressed nicely by how kld allocates
185 * memory for the module bits:
186 * http://fxr.watson.org/fxr/source/kern/link_elf.c?v=RELENG62#L701
187 */
188#if 0
189 pMemFreeBSD->Core.pv = malloc(cb, M_IPRTMOBJ, M_ZERO);
190 if (pMemFreeBSD->Core.pv)
191 {
192 *ppMem = &pMemFreeBSD->Core;
193 return VINF_SUCCESS;
194 }
195 rc = VERR_NO_MEMORY;
196 NOREF(fExecutable);
197
198#else
199 pMemFreeBSD->pObject = vm_object_allocate(OBJT_DEFAULT, cb >> PAGE_SHIFT);
200 if (pMemFreeBSD->pObject)
201 {
202 vm_offset_t MapAddress = vm_map_min(kernel_map);
203 rc = vm_map_find(kernel_map, /* map */
204 pMemFreeBSD->pObject, /* object */
205 0, /* offset */
206 &MapAddress, /* addr (IN/OUT) */
207 cb, /* length */
208 TRUE, /* find_space */
209 fExecutable /* protection */
210 ? VM_PROT_ALL
211 : VM_PROT_RW,
212 VM_PROT_ALL, /* max(_prot) */
213 FALSE); /* cow (copy-on-write) */
214 if (rc == KERN_SUCCESS)
215 {
216 rc = vm_map_wire(kernel_map, /* map */
217 MapAddress, /* start */
218 MapAddress + cb, /* end */
219 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
220 if (rc == KERN_SUCCESS)
221 {
222 pMemFreeBSD->Core.pv = (void *)MapAddress;
223 *ppMem = &pMemFreeBSD->Core;
224 return VINF_SUCCESS;
225 }
226
227 vm_map_remove(kernel_map,
228 MapAddress,
229 MapAddress + cb);
230 }
231 rc = VERR_NO_MEMORY; /** @todo fix translation (borrow from darwin) */
232 }
233 else
234 rc = VERR_NO_MEMORY;
235#endif
236
237 rtR0MemObjDelete(&pMemFreeBSD->Core);
238 return rc;
239}
240
241
242int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
243{
244 /*
245 * Try a Alloc first and see if we get luck, if not try contigmalloc.
246 * Might wish to try find our own pages or something later if this
247 * turns into a problemspot on AMD64 boxes.
248 */
249 int rc = rtR0MemObjNativeAllocPage(ppMem, cb, fExecutable);
250 if (RT_SUCCESS(rc))
251 {
252 size_t iPage = cb >> PAGE_SHIFT;
253 while (iPage-- > 0)
254 if (rtR0MemObjNativeGetPagePhysAddr(*ppMem, iPage) > (_4G - PAGE_SIZE))
255 {
256 RTR0MemObjFree(*ppMem, false);
257 *ppMem = NULL;
258 rc = VERR_NO_MEMORY;
259 break;
260 }
261 }
262 if (RT_FAILURE(rc))
263 rc = rtR0MemObjNativeAllocCont(ppMem, cb, fExecutable);
264 return rc;
265}
266
267
268int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
269{
270 /* create the object. */
271 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_CONT, NULL, cb);
272 if (!pMemFreeBSD)
273 return VERR_NO_MEMORY;
274
275 /* do the allocation. */
276 pMemFreeBSD->Core.pv = contigmalloc(cb, /* size */
277 M_IPRTMOBJ, /* type */
278 M_NOWAIT | M_ZERO, /* flags */
279 0, /* lowest physical address*/
280 _4G-1, /* highest physical address */
281 PAGE_SIZE, /* alignment. */
282 0); /* boundrary */
283 if (pMemFreeBSD->Core.pv)
284 {
285 pMemFreeBSD->Core.u.Cont.Phys = vtophys(pMemFreeBSD->Core.pv);
286 *ppMem = &pMemFreeBSD->Core;
287 return VINF_SUCCESS;
288 }
289
290 NOREF(fExecutable);
291 rtR0MemObjDelete(&pMemFreeBSD->Core);
292 return VERR_NO_MEMORY;
293}
294
295
296int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
297{
298 /** @todo check if there is a more appropriate API somewhere.. */
299
300 /* create the object. */
301 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_CONT, NULL, cb);
302 if (!pMemFreeBSD)
303 return VERR_NO_MEMORY;
304
305 /* do the allocation. */
306 pMemFreeBSD->Core.pv = contigmalloc(cb, /* size */
307 M_IPRTMOBJ, /* type */
308 M_NOWAIT | M_ZERO, /* flags */
309 0, /* lowest physical address*/
310 PhysHighest, /* highest physical address */
311 PAGE_SIZE, /* alignment. */
312 0); /* boundrary */
313 if (pMemFreeBSD->Core.pv)
314 {
315 pMemFreeBSD->Core.u.Cont.Phys = vtophys(pMemFreeBSD->Core.pv);
316 *ppMem = &pMemFreeBSD->Core;
317 return VINF_SUCCESS;
318 }
319
320 rtR0MemObjDelete(&pMemFreeBSD->Core);
321 return VERR_NO_MEMORY;
322}
323
324
325int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
326{
327 /** @todo rtR0MemObjNativeAllocPhys / freebsd */
328 return VERR_NOT_SUPPORTED;
329}
330
331
332int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
333{
334 /* create the object. */
335 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_PHYS, NULL, cb);
336 if (!pMemFreeBSD)
337 return VERR_NO_MEMORY;
338
339 /* there is no allocation here, it needs to be mapped somewhere first. */
340 pMemFreeBSD->Core.u.Phys.fAllocated = false;
341 pMemFreeBSD->Core.u.Phys.PhysBase = Phys;
342 *ppMem = &pMemFreeBSD->Core;
343 return VINF_SUCCESS;
344}
345
346
347int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
348{
349 int rc;
350
351 /* create the object. */
352 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
353 if (!pMemFreeBSD)
354 return VERR_NO_MEMORY;
355
356 /*
357 * We could've used vslock here, but we don't wish to be subject to
358 * resource usage restrictions, so we'll call vm_map_wire directly.
359 */
360 rc = vm_map_wire(&((struct proc *)R0Process)->p_vmspace->vm_map, /* the map */
361 (vm_offset_t)R3Ptr, /* start */
362 (vm_offset_t)R3Ptr + cb, /* end */
363 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES); /* flags */
364 if (rc == KERN_SUCCESS)
365 {
366 pMemFreeBSD->Core.u.Lock.R0Process = R0Process;
367 *ppMem = &pMemFreeBSD->Core;
368 return VINF_SUCCESS;
369 }
370 rtR0MemObjDelete(&pMemFreeBSD->Core);
371 return VERR_NO_MEMORY;/** @todo fix mach -> vbox error conversion for freebsd. */
372}
373
374
375int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb)
376{
377 int rc;
378
379 /* create the object. */
380 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_LOCK, pv, cb);
381 if (!pMemFreeBSD)
382 return VERR_NO_MEMORY;
383
384 /* lock the memory */
385 rc = vm_map_wire(kernel_map, /* the map */
386 (vm_offset_t)pv, /* start */
387 (vm_offset_t)pv + cb, /* end */
388 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); /* flags - SYSTEM? */
389 if (rc == KERN_SUCCESS)
390 {
391 pMemFreeBSD->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
392 *ppMem = &pMemFreeBSD->Core;
393 return VINF_SUCCESS;
394 }
395 rtR0MemObjDelete(&pMemFreeBSD->Core);
396 return VERR_NO_MEMORY;/** @todo fix mach -> vbox error conversion for freebsd. */
397}
398
399
400/**
401 * Worker for the two virtual address space reservers.
402 *
403 * We're leaning on the examples provided by mmap and vm_mmap in vm_mmap.c here.
404 */
405static int rtR0MemObjNativeReserveInMap(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process, vm_map_t pMap)
406{
407 int rc;
408
409 /*
410 * The pvFixed address range must be within the VM space when specified.
411 */
412 if (pvFixed != (void *)-1
413 && ( (vm_offset_t)pvFixed < vm_map_min(pMap)
414 || (vm_offset_t)pvFixed + cb > vm_map_max(pMap)))
415 return VERR_INVALID_PARAMETER;
416
417 /*
418 * Create the object.
419 */
420 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_RES_VIRT, NULL, cb);
421 if (!pMemFreeBSD)
422 return VERR_NO_MEMORY;
423
424 /*
425 * Allocate an empty VM object and map it into the requested map.
426 */
427 pMemFreeBSD->pObject = vm_object_allocate(OBJT_DEFAULT, cb >> PAGE_SHIFT);
428 if (pMemFreeBSD->pObject)
429 {
430 vm_offset_t MapAddress = pvFixed != (void *)-1
431 ? (vm_offset_t)pvFixed
432 : vm_map_min(pMap);
433 if (pvFixed)
434 vm_map_remove(pMap,
435 MapAddress,
436 MapAddress + cb);
437
438 rc = vm_map_find(pMap, /* map */
439 pMemFreeBSD->pObject, /* object */
440 0, /* offset */
441 &MapAddress, /* addr (IN/OUT) */
442 cb, /* length */
443 pvFixed == (void *)-1, /* find_space */
444 VM_PROT_NONE, /* protection */
445 VM_PROT_ALL, /* max(_prot) ?? */
446 0); /* cow (copy-on-write) */
447 if (rc == KERN_SUCCESS)
448 {
449 if (R0Process != NIL_RTR0PROCESS)
450 {
451 rc = vm_map_inherit(pMap,
452 MapAddress,
453 MapAddress + cb,
454 VM_INHERIT_SHARE);
455 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
456 }
457 pMemFreeBSD->Core.pv = (void *)MapAddress;
458 pMemFreeBSD->Core.u.ResVirt.R0Process = R0Process;
459 *ppMem = &pMemFreeBSD->Core;
460 return VINF_SUCCESS;
461 }
462 vm_object_deallocate(pMemFreeBSD->pObject);
463 rc = VERR_NO_MEMORY; /** @todo fix translation (borrow from darwin) */
464 }
465 else
466 rc = VERR_NO_MEMORY;
467 rtR0MemObjDelete(&pMemFreeBSD->Core);
468 return rc;
469
470}
471
472int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
473{
474 return rtR0MemObjNativeReserveInMap(ppMem, pvFixed, cb, uAlignment, NIL_RTR0PROCESS, kernel_map);
475}
476
477
478int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
479{
480 return rtR0MemObjNativeReserveInMap(ppMem, (void *)R3PtrFixed, cb, uAlignment, R0Process,
481 &((struct proc *)R0Process)->p_vmspace->vm_map);
482}
483
484
485int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
486 unsigned fProt, size_t offSub, size_t cbSub)
487{
488 AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
489 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
490
491/* Phys: see pmap_mapdev in i386/i386/pmap.c (http://fxr.watson.org/fxr/source/i386/i386/pmap.c?v=RELENG62#L2860) */
492
493#if 0
494/** @todo finish the implementation. */
495
496 int rc;
497 void *pvR0 = NULL;
498 PRTR0MEMOBJFREEBSD pMemToMapOs2 = (PRTR0MEMOBJFREEBSD)pMemToMap;
499 switch (pMemToMapOs2->Core.enmType)
500 {
501 /*
502 * These has kernel mappings.
503 */
504 case RTR0MEMOBJTYPE_PAGE:
505 case RTR0MEMOBJTYPE_LOW:
506 case RTR0MEMOBJTYPE_CONT:
507 pvR0 = pMemToMapOs2->Core.pv;
508 break;
509
510 case RTR0MEMOBJTYPE_PHYS_NC:
511 case RTR0MEMOBJTYPE_PHYS:
512 pvR0 = pMemToMapOs2->Core.pv;
513 if (!pvR0)
514 {
515 /* no ring-0 mapping, so allocate a mapping in the process. */
516 AssertMsgReturn(uAlignment == PAGE_SIZE, ("%#zx\n", uAlignment), VERR_NOT_SUPPORTED);
517 AssertMsgReturn(fProt & RTMEM_PROT_WRITE, ("%#x\n", fProt), VERR_NOT_SUPPORTED);
518 Assert(!pMemToMapOs2->Core.u.Phys.fAllocated);
519 ULONG ulPhys = pMemToMapOs2->Core.u.Phys.PhysBase;
520 rc = KernVMAlloc(pMemToMapOs2->Core.cb, VMDHA_PHYS, &pvR0, (PPVOID)&ulPhys, NULL);
521 if (rc)
522 return RTErrConvertFromOS2(rc);
523 pMemToMapOs2->Core.pv = pvR0;
524 }
525 break;
526
527 case RTR0MEMOBJTYPE_LOCK:
528 if (pMemToMapOs2->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
529 return VERR_NOT_SUPPORTED; /** @todo implement this... */
530 pvR0 = pMemToMapOs2->Core.pv;
531 break;
532
533 case RTR0MEMOBJTYPE_RES_VIRT:
534 case RTR0MEMOBJTYPE_MAPPING:
535 default:
536 AssertMsgFailed(("enmType=%d\n", pMemToMapOs2->Core.enmType));
537 return VERR_INTERNAL_ERROR;
538 }
539
540 /*
541 * Create a dummy mapping object for it.
542 *
543 * All mappings are read/write/execute in OS/2 and there isn't
544 * any cache options, so sharing is ok. And the main memory object
545 * isn't actually freed until all the mappings have been freed up
546 * (reference counting).
547 */
548 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJOS2, Lock), RTR0MEMOBJTYPE_MAPPING, pvR0, pMemToMapOs2->Core.cb);
549 if (pMemFreeBSD)
550 {
551 pMemFreeBSD->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
552 *ppMem = &pMemFreeBSD->Core;
553 return VINF_SUCCESS;
554 }
555 return VERR_NO_MEMORY;
556#endif
557 return VERR_NOT_IMPLEMENTED;
558}
559
560
561/* see http://markmail.org/message/udhq33tefgtyfozs */
562int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
563{
564 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
565 AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
566
567 int rc;
568 vm_object_t pObjectToMap = ((PRTR0MEMOBJFREEBSD)pMemToMap)->pObject;
569 struct proc *pProc = (struct proc *)R0Process;
570 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
571
572 /* calc protection */
573 vm_prot_t ProtectionFlags = 0;
574 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
575 ProtectionFlags = VM_PROT_NONE;
576 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
577 ProtectionFlags |= VM_PROT_READ;
578 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
579 ProtectionFlags |= VM_PROT_WRITE;
580 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
581 ProtectionFlags |= VM_PROT_EXECUTE;
582
583 /* calc mapping address */
584 PROC_LOCK(pProc);
585 vm_offset_t AddrR3 = round_page((vm_offset_t)pProc->p_vmspace->vm_daddr + lim_max(pProc, RLIMIT_DATA));
586 PROC_UNLOCK(pProc);
587
588 /*
589 * Mapping into R3 is easy if the mem object has a associated VM object.
590 * If there is not such an object we have to get it from the address.
591 */
592 if (!pObjectToMap)
593 {
594 vm_object_t pObjectNew = vm_object_allocate(OBJT_PHYS, pMemToMap->cb >> PAGE_SHIFT);
595 if (pObjectNew)
596 {
597 /* Insert the object in the map. */
598 rc = vm_map_find(pProcMap, /* Map to insert the object in */
599 pObjectNew , /* Object to map */
600 0, /* Start offset in the object */
601 &AddrR3, /* Start address IN/OUT */
602 pMemToMap->cb, /* Size of the mapping */
603 TRUE, /* Whether a suitable address should be searched for first */
604 ProtectionFlags, /* protection flags */
605 VM_PROT_ALL, /* Maximum protection flags */
606 0); /* Copy on write */
607 if (rc == KERN_SUCCESS)
608 {
609 size_t cLeft = pMemToMap->cb >> PAGE_SHIFT;
610 vm_offset_t AddrToMap = (vm_offset_t)pMemToMap->pv;
611 pmap_t pPhysicalMap = pProcMap->pmap;
612 vm_offset_t AddrR3Dst = AddrR3;
613
614 /* Insert the memory page by page into the mapping. */
615 while (cLeft-- > 0)
616 {
617 vm_page_t Page = PHYS_TO_VM_PAGE(vtophys(AddrToMap));
618
619 pmap_enter(pPhysicalMap, AddrR3Dst, Page, ProtectionFlags, TRUE);
620 AddrToMap += PAGE_SIZE;
621 AddrR3Dst += PAGE_SIZE;
622 }
623 pObjectToMap = pObjectNew;
624 }
625 else
626 vm_object_deallocate(pObjectNew);
627 }
628 else
629 {
630 AssertMsgFailed(("Could not allocate VM object\n"));
631 rc = 1; /* @todo fix */
632 }
633 }
634 else
635 {
636 /*
637 * Reference the object. If this isn't done the object will removed from kernel space
638 * if the mapping is destroyed.
639 */
640 vm_object_reference(pObjectToMap);
641
642 rc = vm_map_find(pProcMap, /* Map to insert the object in */
643 pObjectToMap, /* Object to map */
644 0, /* Start offset in the object */
645 &AddrR3, /* Start address IN/OUT */
646 pMemToMap->cb, /* Size of the mapping */
647 TRUE, /* Whether a suitable address should be searched for first */
648 ProtectionFlags, /* protection flags */
649 VM_PROT_ALL, /* Maximum protection flags */
650 0); /* Copy on write */
651 }
652
653 if (rc == KERN_SUCCESS)
654 {
655 /*
656 * Create a mapping object for it.
657 */
658 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(RTR0MEMOBJFREEBSD),
659 RTR0MEMOBJTYPE_MAPPING,
660 (void *)AddrR3,
661 pMemToMap->cb);
662 if (pMemFreeBSD)
663 {
664 Assert((vm_offset_t)pMemFreeBSD->Core.pv == AddrR3);
665 pMemFreeBSD->Core.u.Mapping.R0Process = R0Process;
666 pMemFreeBSD->pMappingObject = pObjectToMap;
667 *ppMem = &pMemFreeBSD->Core;
668 return VINF_SUCCESS;
669 }
670
671 rc = vm_map_remove(pProcMap, ((vm_offset_t)AddrR3), ((vm_offset_t)AddrR3) + pMemToMap->cb);
672 AssertMsg(rc == KERN_SUCCESS, ("Deleting mapping failed\n"));
673 }
674
675 if (pObjectToMap)
676 vm_object_deallocate(pObjectToMap);
677
678 return VERR_NO_MEMORY;
679}
680
681
682RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
683{
684 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)pMem;
685
686 switch (pMemFreeBSD->Core.enmType)
687 {
688 case RTR0MEMOBJTYPE_LOCK:
689 if ( pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS
690 && pMemFreeBSD->Core.u.Lock.R0Process != (RTR0PROCESS)curproc)
691 {
692 /* later */
693 return NIL_RTHCPHYS;
694 }
695 /* fall thru*/
696 case RTR0MEMOBJTYPE_PAGE:
697 case RTR0MEMOBJTYPE_MAPPING:
698 {
699 uint8_t *pb = (uint8_t *)pMemFreeBSD->Core.pv + (iPage << PAGE_SHIFT);
700 return vtophys(pb);
701 }
702
703 case RTR0MEMOBJTYPE_CONT:
704 return pMemFreeBSD->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
705
706 case RTR0MEMOBJTYPE_PHYS:
707 return pMemFreeBSD->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
708
709 case RTR0MEMOBJTYPE_PHYS_NC:
710 case RTR0MEMOBJTYPE_RES_VIRT:
711 case RTR0MEMOBJTYPE_LOW:
712 default:
713 return NIL_RTHCPHYS;
714 }
715}
716
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette