VirtualBox

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

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

memobj-r0drv-freebsd.c: nit: no else after return.

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