VirtualBox

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

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

iprt/memobj.h: Added RTR0MemObjProtect, only implemented for darwin.

  • 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 20525 2009-06-13 20:13:33Z vboxsync $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects, 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
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 contigfree(pMemFreeBSD->Core.pv, pMemFreeBSD->Core.cb, 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 size_t cPages = cb >> PAGE_SHIFT;
178
179 /* create the object. */
180 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_PAGE, NULL, cb);
181 if (!pMemFreeBSD)
182 return VERR_NO_MEMORY;
183
184 pMemFreeBSD->pObject = vm_object_allocate(OBJT_DEFAULT, cPages);
185 if (pMemFreeBSD->pObject)
186 {
187 vm_offset_t MapAddress = vm_map_min(kernel_map);
188 rc = vm_map_find(kernel_map, /* map */
189 pMemFreeBSD->pObject, /* object */
190 0, /* offset */
191 &MapAddress, /* addr (IN/OUT) */
192 cb, /* length */
193 TRUE, /* find_space */
194 fExecutable /* protection */
195 ? VM_PROT_ALL
196 : VM_PROT_RW,
197 VM_PROT_ALL, /* max(_prot) */
198 FALSE); /* cow (copy-on-write) */
199 if (rc == KERN_SUCCESS)
200 {
201 vm_offset_t AddressDst = MapAddress;
202
203 rc = VINF_SUCCESS;
204
205 VM_OBJECT_LOCK(pMemFreeBSD->pObject);
206 for (size_t iPage = 0; iPage < cPages; iPage++)
207 {
208 vm_pindex_t PageIndex = OFF_TO_IDX(AddressDst);
209 vm_page_t pPage;
210
211 pPage = vm_page_alloc(pMemFreeBSD->pObject, PageIndex,
212 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM |
213 VM_ALLOC_WIRED);
214 if (pPage)
215 {
216 vm_page_lock_queues();
217 vm_page_wire(pPage);
218 vm_page_unlock_queues();
219 /* Put the page into the page table now. */
220#if __FreeBSD_version >= 701105
221 pmap_enter(kernel_map->pmap, AddressDst, VM_PROT_NONE, pPage,
222 fExecutable
223 ? VM_PROT_ALL
224 : VM_PROT_RW,
225 TRUE);
226#else
227 pmap_enter(kernel_map->pmap, AddressDst, pPage,
228 fExecutable
229 ? VM_PROT_ALL
230 : VM_PROT_RW,
231 TRUE);
232#endif
233 }
234 else
235 {
236 /*
237 * Allocation failed. vm_map_remove will remove any
238 * page already alocated.
239 */
240 rc = VERR_NO_MEMORY;
241 break;
242 }
243 AddressDst += PAGE_SIZE;
244 }
245 VM_OBJECT_UNLOCK(pMemFreeBSD->pObject);
246
247 if (rc == VINF_SUCCESS)
248 {
249 pMemFreeBSD->Core.pv = (void *)MapAddress;
250 *ppMem = &pMemFreeBSD->Core;
251 return VINF_SUCCESS;
252 }
253
254 vm_map_remove(kernel_map,
255 MapAddress,
256 MapAddress + cb);
257 }
258 rc = VERR_NO_MEMORY; /** @todo fix translation (borrow from darwin) */
259 }
260 else
261 rc = VERR_NO_MEMORY;
262
263 rtR0MemObjDelete(&pMemFreeBSD->Core);
264 return rc;
265}
266
267
268int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
269{
270 /*
271 * Try a Alloc first and see if we get luck, if not try contigmalloc.
272 * Might wish to try find our own pages or something later if this
273 * turns into a problemspot on AMD64 boxes.
274 */
275 int rc = rtR0MemObjNativeAllocPage(ppMem, cb, fExecutable);
276 if (RT_SUCCESS(rc))
277 {
278 size_t iPage = cb >> PAGE_SHIFT;
279 while (iPage-- > 0)
280 if (rtR0MemObjNativeGetPagePhysAddr(*ppMem, iPage) > (_4G - PAGE_SIZE))
281 {
282 RTR0MemObjFree(*ppMem, false);
283 *ppMem = NULL;
284 rc = VERR_NO_MEMORY;
285 break;
286 }
287 }
288 if (RT_FAILURE(rc))
289 rc = rtR0MemObjNativeAllocCont(ppMem, cb, fExecutable);
290 return rc;
291}
292
293
294int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
295{
296 /* create the object. */
297 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_CONT, NULL, cb);
298 if (!pMemFreeBSD)
299 return VERR_NO_MEMORY;
300
301 /* do the allocation. */
302 pMemFreeBSD->Core.pv = contigmalloc(cb, /* size */
303 M_IPRTMOBJ, /* type */
304 M_NOWAIT | M_ZERO, /* flags */
305 0, /* lowest physical address*/
306 _4G-1, /* highest physical address */
307 PAGE_SIZE, /* alignment. */
308 0); /* boundrary */
309 if (pMemFreeBSD->Core.pv)
310 {
311 pMemFreeBSD->Core.u.Cont.Phys = vtophys(pMemFreeBSD->Core.pv);
312 *ppMem = &pMemFreeBSD->Core;
313 return VINF_SUCCESS;
314 }
315
316 NOREF(fExecutable);
317 rtR0MemObjDelete(&pMemFreeBSD->Core);
318 return VERR_NO_MEMORY;
319}
320
321
322int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
323{
324 /** @todo check if there is a more appropriate API somewhere.. */
325
326 /* create the object. */
327 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_CONT, NULL, cb);
328 if (!pMemFreeBSD)
329 return VERR_NO_MEMORY;
330
331 /* do the allocation. */
332 pMemFreeBSD->Core.pv = contigmalloc(cb, /* size */
333 M_IPRTMOBJ, /* type */
334 M_NOWAIT | M_ZERO, /* flags */
335 0, /* lowest physical address*/
336 PhysHighest, /* highest physical address */
337 PAGE_SIZE, /* alignment. */
338 0); /* boundrary */
339 if (pMemFreeBSD->Core.pv)
340 {
341 pMemFreeBSD->Core.u.Cont.Phys = vtophys(pMemFreeBSD->Core.pv);
342 *ppMem = &pMemFreeBSD->Core;
343 return VINF_SUCCESS;
344 }
345
346 rtR0MemObjDelete(&pMemFreeBSD->Core);
347 return VERR_NO_MEMORY;
348}
349
350
351int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
352{
353 /** @todo rtR0MemObjNativeAllocPhys / freebsd */
354 return VERR_NOT_SUPPORTED;
355}
356
357
358int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
359{
360 /* create the object. */
361 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_PHYS, NULL, cb);
362 if (!pMemFreeBSD)
363 return VERR_NO_MEMORY;
364
365 /* there is no allocation here, it needs to be mapped somewhere first. */
366 pMemFreeBSD->Core.u.Phys.fAllocated = false;
367 pMemFreeBSD->Core.u.Phys.PhysBase = Phys;
368 *ppMem = &pMemFreeBSD->Core;
369 return VINF_SUCCESS;
370}
371
372
373int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
374{
375 int rc;
376
377 /* create the object. */
378 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
379 if (!pMemFreeBSD)
380 return VERR_NO_MEMORY;
381
382 /*
383 * We could've used vslock here, but we don't wish to be subject to
384 * resource usage restrictions, so we'll call vm_map_wire directly.
385 */
386 rc = vm_map_wire(&((struct proc *)R0Process)->p_vmspace->vm_map, /* the map */
387 (vm_offset_t)R3Ptr, /* start */
388 (vm_offset_t)R3Ptr + cb, /* end */
389 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES); /* flags */
390 if (rc == KERN_SUCCESS)
391 {
392 pMemFreeBSD->Core.u.Lock.R0Process = R0Process;
393 *ppMem = &pMemFreeBSD->Core;
394 return VINF_SUCCESS;
395 }
396 rtR0MemObjDelete(&pMemFreeBSD->Core);
397 return VERR_NO_MEMORY;/** @todo fix mach -> vbox error conversion for freebsd. */
398}
399
400
401int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb)
402{
403 int rc;
404
405 /* create the object. */
406 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_LOCK, pv, cb);
407 if (!pMemFreeBSD)
408 return VERR_NO_MEMORY;
409
410 /* lock the memory */
411 rc = vm_map_wire(kernel_map, /* the map */
412 (vm_offset_t)pv, /* start */
413 (vm_offset_t)pv + cb, /* end */
414 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); /* flags - SYSTEM? */
415 if (rc == KERN_SUCCESS)
416 {
417 pMemFreeBSD->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
418 *ppMem = &pMemFreeBSD->Core;
419 return VINF_SUCCESS;
420 }
421 rtR0MemObjDelete(&pMemFreeBSD->Core);
422 return VERR_NO_MEMORY;/** @todo fix mach -> vbox error conversion for freebsd. */
423}
424
425
426/**
427 * Worker for the two virtual address space reservers.
428 *
429 * We're leaning on the examples provided by mmap and vm_mmap in vm_mmap.c here.
430 */
431static int rtR0MemObjNativeReserveInMap(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process, vm_map_t pMap)
432{
433 int rc;
434
435 /*
436 * The pvFixed address range must be within the VM space when specified.
437 */
438 if (pvFixed != (void *)-1
439 && ( (vm_offset_t)pvFixed < vm_map_min(pMap)
440 || (vm_offset_t)pvFixed + cb > vm_map_max(pMap)))
441 return VERR_INVALID_PARAMETER;
442
443 /*
444 * Create the object.
445 */
446 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_RES_VIRT, NULL, cb);
447 if (!pMemFreeBSD)
448 return VERR_NO_MEMORY;
449
450 /*
451 * Allocate an empty VM object and map it into the requested map.
452 */
453 pMemFreeBSD->pObject = vm_object_allocate(OBJT_DEFAULT, cb >> PAGE_SHIFT);
454 if (pMemFreeBSD->pObject)
455 {
456 vm_offset_t MapAddress = pvFixed != (void *)-1
457 ? (vm_offset_t)pvFixed
458 : vm_map_min(pMap);
459 if (pvFixed)
460 vm_map_remove(pMap,
461 MapAddress,
462 MapAddress + cb);
463
464 rc = vm_map_find(pMap, /* map */
465 pMemFreeBSD->pObject, /* object */
466 0, /* offset */
467 &MapAddress, /* addr (IN/OUT) */
468 cb, /* length */
469 pvFixed == (void *)-1, /* find_space */
470 VM_PROT_NONE, /* protection */
471 VM_PROT_ALL, /* max(_prot) ?? */
472 0); /* cow (copy-on-write) */
473 if (rc == KERN_SUCCESS)
474 {
475 if (R0Process != NIL_RTR0PROCESS)
476 {
477 rc = vm_map_inherit(pMap,
478 MapAddress,
479 MapAddress + cb,
480 VM_INHERIT_SHARE);
481 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
482 }
483 pMemFreeBSD->Core.pv = (void *)MapAddress;
484 pMemFreeBSD->Core.u.ResVirt.R0Process = R0Process;
485 *ppMem = &pMemFreeBSD->Core;
486 return VINF_SUCCESS;
487 }
488 vm_object_deallocate(pMemFreeBSD->pObject);
489 rc = VERR_NO_MEMORY; /** @todo fix translation (borrow from darwin) */
490 }
491 else
492 rc = VERR_NO_MEMORY;
493 rtR0MemObjDelete(&pMemFreeBSD->Core);
494 return rc;
495
496}
497
498int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
499{
500 return rtR0MemObjNativeReserveInMap(ppMem, pvFixed, cb, uAlignment, NIL_RTR0PROCESS, kernel_map);
501}
502
503
504int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
505{
506 return rtR0MemObjNativeReserveInMap(ppMem, (void *)R3PtrFixed, cb, uAlignment, R0Process,
507 &((struct proc *)R0Process)->p_vmspace->vm_map);
508}
509
510
511int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
512 unsigned fProt, size_t offSub, size_t cbSub)
513{
514 AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
515 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
516
517/* Phys: see pmap_mapdev in i386/i386/pmap.c (http://fxr.watson.org/fxr/source/i386/i386/pmap.c?v=RELENG62#L2860) */
518
519#if 0
520/** @todo finish the implementation. */
521
522 int rc;
523 void *pvR0 = NULL;
524 PRTR0MEMOBJFREEBSD pMemToMapOs2 = (PRTR0MEMOBJFREEBSD)pMemToMap;
525 switch (pMemToMapOs2->Core.enmType)
526 {
527 /*
528 * These has kernel mappings.
529 */
530 case RTR0MEMOBJTYPE_PAGE:
531 case RTR0MEMOBJTYPE_LOW:
532 case RTR0MEMOBJTYPE_CONT:
533 pvR0 = pMemToMapOs2->Core.pv;
534 break;
535
536 case RTR0MEMOBJTYPE_PHYS_NC:
537 case RTR0MEMOBJTYPE_PHYS:
538 pvR0 = pMemToMapOs2->Core.pv;
539 if (!pvR0)
540 {
541 /* no ring-0 mapping, so allocate a mapping in the process. */
542 AssertMsgReturn(uAlignment == PAGE_SIZE, ("%#zx\n", uAlignment), VERR_NOT_SUPPORTED);
543 AssertMsgReturn(fProt & RTMEM_PROT_WRITE, ("%#x\n", fProt), VERR_NOT_SUPPORTED);
544 Assert(!pMemToMapOs2->Core.u.Phys.fAllocated);
545 ULONG ulPhys = pMemToMapOs2->Core.u.Phys.PhysBase;
546 rc = KernVMAlloc(pMemToMapOs2->Core.cb, VMDHA_PHYS, &pvR0, (PPVOID)&ulPhys, NULL);
547 if (rc)
548 return RTErrConvertFromOS2(rc);
549 pMemToMapOs2->Core.pv = pvR0;
550 }
551 break;
552
553 case RTR0MEMOBJTYPE_LOCK:
554 if (pMemToMapOs2->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
555 return VERR_NOT_SUPPORTED; /** @todo implement this... */
556 pvR0 = pMemToMapOs2->Core.pv;
557 break;
558
559 case RTR0MEMOBJTYPE_RES_VIRT:
560 case RTR0MEMOBJTYPE_MAPPING:
561 default:
562 AssertMsgFailed(("enmType=%d\n", pMemToMapOs2->Core.enmType));
563 return VERR_INTERNAL_ERROR;
564 }
565
566 /*
567 * Create a dummy mapping object for it.
568 *
569 * All mappings are read/write/execute in OS/2 and there isn't
570 * any cache options, so sharing is ok. And the main memory object
571 * isn't actually freed until all the mappings have been freed up
572 * (reference counting).
573 */
574 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJOS2, Lock), RTR0MEMOBJTYPE_MAPPING, pvR0, pMemToMapOs2->Core.cb);
575 if (pMemFreeBSD)
576 {
577 pMemFreeBSD->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
578 *ppMem = &pMemFreeBSD->Core;
579 return VINF_SUCCESS;
580 }
581 return VERR_NO_MEMORY;
582#endif
583 return VERR_NOT_IMPLEMENTED;
584}
585
586
587/* see http://markmail.org/message/udhq33tefgtyfozs */
588int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
589{
590 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
591 AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
592
593 int rc;
594 vm_object_t pObjectToMap = ((PRTR0MEMOBJFREEBSD)pMemToMap)->pObject;
595 struct proc *pProc = (struct proc *)R0Process;
596 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
597
598 /* calc protection */
599 vm_prot_t ProtectionFlags = 0;
600 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
601 ProtectionFlags = VM_PROT_NONE;
602 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
603 ProtectionFlags |= VM_PROT_READ;
604 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
605 ProtectionFlags |= VM_PROT_WRITE;
606 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
607 ProtectionFlags |= VM_PROT_EXECUTE;
608
609 /* calc mapping address */
610 PROC_LOCK(pProc);
611 vm_offset_t AddrR3 = round_page((vm_offset_t)pProc->p_vmspace->vm_daddr + lim_max(pProc, RLIMIT_DATA));
612 PROC_UNLOCK(pProc);
613
614 vm_object_t pObjectNew = vm_object_allocate(OBJT_PHYS, pMemToMap->cb >> PAGE_SHIFT);
615 if (!RT_UNLIKELY(pObjectNew))
616 return VERR_NO_MEMORY;
617
618 /* Insert the object in the map. */
619 rc = vm_map_find(pProcMap, /* Map to insert the object in */
620 pObjectNew , /* Object to map */
621 0, /* Start offset in the object */
622 &AddrR3, /* Start address IN/OUT */
623 pMemToMap->cb, /* Size of the mapping */
624 TRUE, /* Whether a suitable address should be searched for first */
625 ProtectionFlags, /* protection flags */
626 VM_PROT_ALL, /* Maximum protection flags */
627 0); /* Copy on write */
628
629 /* Map the memory page by page into the destination map. */
630 if (rc == KERN_SUCCESS)
631 {
632 size_t cLeft = pMemToMap->cb >> PAGE_SHIFT;
633 vm_offset_t AddrToMap = (vm_offset_t)pMemToMap->pv;
634 pmap_t pPhysicalMap = pProcMap->pmap;
635 vm_offset_t AddrR3Dst = AddrR3;
636
637 /* Insert the memory page by page into the mapping. */
638 while (cLeft-- > 0)
639 {
640 vm_page_t Page = PHYS_TO_VM_PAGE(vtophys(AddrToMap));
641
642#if __FreeBSD_version >= 701105
643 pmap_enter(pPhysicalMap, AddrR3Dst, VM_PROT_NONE, Page, ProtectionFlags, TRUE);
644#else
645 pmap_enter(pPhysicalMap, AddrR3Dst, Page, ProtectionFlags, TRUE);
646#endif
647 AddrToMap += PAGE_SIZE;
648 AddrR3Dst += PAGE_SIZE;
649 }
650 pObjectToMap = pObjectNew;
651 }
652 else
653 vm_object_deallocate(pObjectNew);
654
655 if (rc == KERN_SUCCESS)
656 {
657 /*
658 * Create a mapping object for it.
659 */
660 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(RTR0MEMOBJFREEBSD),
661 RTR0MEMOBJTYPE_MAPPING,
662 (void *)AddrR3,
663 pMemToMap->cb);
664 if (pMemFreeBSD)
665 {
666 Assert((vm_offset_t)pMemFreeBSD->Core.pv == AddrR3);
667 pMemFreeBSD->Core.u.Mapping.R0Process = R0Process;
668 pMemFreeBSD->pMappingObject = pObjectToMap;
669 *ppMem = &pMemFreeBSD->Core;
670 return VINF_SUCCESS;
671 }
672
673 rc = vm_map_remove(pProcMap, ((vm_offset_t)AddrR3), ((vm_offset_t)AddrR3) + pMemToMap->cb);
674 AssertMsg(rc == KERN_SUCCESS, ("Deleting mapping failed\n"));
675 }
676
677 if (pObjectToMap)
678 vm_object_deallocate(pObjectToMap);
679
680 return VERR_NO_MEMORY;
681}
682
683
684int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
685{
686 NOREF(pMem);
687 NOREF(offSub);
688 NOREF(cbSub);
689 NOREF(fProt);
690 return VERR_NOT_SUPPORTED;
691}
692
693
694RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
695{
696 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)pMem;
697
698 switch (pMemFreeBSD->Core.enmType)
699 {
700 case RTR0MEMOBJTYPE_LOCK:
701 if ( pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS
702 && pMemFreeBSD->Core.u.Lock.R0Process != (RTR0PROCESS)curproc)
703 {
704 /* later */
705 return NIL_RTHCPHYS;
706 }
707 /* fall thru*/
708 case RTR0MEMOBJTYPE_PAGE:
709 case RTR0MEMOBJTYPE_MAPPING:
710 {
711 uint8_t *pb = (uint8_t *)pMemFreeBSD->Core.pv + (iPage << PAGE_SHIFT);
712 return vtophys(pb);
713 }
714
715 case RTR0MEMOBJTYPE_CONT:
716 return pMemFreeBSD->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
717
718 case RTR0MEMOBJTYPE_PHYS:
719 return pMemFreeBSD->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
720
721 case RTR0MEMOBJTYPE_PHYS_NC:
722 case RTR0MEMOBJTYPE_RES_VIRT:
723 case RTR0MEMOBJTYPE_LOW:
724 default:
725 return NIL_RTHCPHYS;
726 }
727}
728
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