VirtualBox

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

Last change on this file since 24181 was 23610, checked in by vboxsync, 15 years ago

IPRT,VMM,SUPDrv,VBGLR0: Added a parameter to RTR0MemObjLockUser/Kernel that indicates read/write intent so we can correctly lock readonly memory on Windows and OS/2. (Guest property strings, see #4238.)

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