VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/haiku/memobj-r0drv-haiku.c@ 45733

Last change on this file since 45733 was 43403, checked in by vboxsync, 12 years ago

r0drv/haiku: cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.6 KB
Line 
1/* $Id: memobj-r0drv-haiku.c 43403 2012-09-22 11:48:24Z vboxsync $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects, Haiku.
4 */
5
6/*
7 * Copyright (C) 2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "the-haiku-kernel.h"
32
33#include <iprt/memobj.h>
34#include <iprt/mem.h>
35#include <iprt/err.h>
36#include <iprt/assert.h>
37#include <iprt/log.h>
38#include <iprt/param.h>
39#include <iprt/process.h>
40#include "internal/memobj.h"
41
42/*******************************************************************************
43* Structures and Typedefs *
44*******************************************************************************/
45/**
46 * The Haiku version of the memory object structure.
47 */
48typedef struct RTR0MEMOBJHAIKU
49{
50 /** The core structure. */
51 RTR0MEMOBJINTERNAL Core;
52 /** Area identifier */
53 area_id AreaId;
54} RTR0MEMOBJHAIKU, *PRTR0MEMOBJHAIKU;
55
56
57//MALLOC_DEFINE(M_IPRTMOBJ, "iprtmobj", "IPRT - R0MemObj");
58#if 0
59/**
60 * Gets the virtual memory map the specified object is mapped into.
61 *
62 * @returns VM map handle on success, NULL if no map.
63 * @param pMem The memory object.
64 */
65static vm_map_t rtR0MemObjHaikuGetMap(PRTR0MEMOBJINTERNAL pMem)
66{
67 switch (pMem->enmType)
68 {
69 case RTR0MEMOBJTYPE_PAGE:
70 case RTR0MEMOBJTYPE_LOW:
71 case RTR0MEMOBJTYPE_CONT:
72 return kernel_map;
73
74 case RTR0MEMOBJTYPE_PHYS:
75 case RTR0MEMOBJTYPE_PHYS_NC:
76 return NULL; /* pretend these have no mapping atm. */
77
78 case RTR0MEMOBJTYPE_LOCK:
79 return pMem->u.Lock.R0Process == NIL_RTR0PROCESS
80 ? kernel_map
81 : &((struct proc *)pMem->u.Lock.R0Process)->p_vmspace->vm_map;
82
83 case RTR0MEMOBJTYPE_RES_VIRT:
84 return pMem->u.ResVirt.R0Process == NIL_RTR0PROCESS
85 ? kernel_map
86 : &((struct proc *)pMem->u.ResVirt.R0Process)->p_vmspace->vm_map;
87
88 case RTR0MEMOBJTYPE_MAPPING:
89 return pMem->u.Mapping.R0Process == NIL_RTR0PROCESS
90 ? kernel_map
91 : &((struct proc *)pMem->u.Mapping.R0Process)->p_vmspace->vm_map;
92
93 default:
94 return NULL;
95 }
96}
97#endif
98
99
100int rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
101{
102 PRTR0MEMOBJHAIKU pMemHaiku = (PRTR0MEMOBJHAIKU)pMem;
103 int rc = B_OK;
104
105 switch (pMemHaiku->Core.enmType)
106 {
107 case RTR0MEMOBJTYPE_PAGE:
108 case RTR0MEMOBJTYPE_LOW:
109 case RTR0MEMOBJTYPE_CONT:
110 case RTR0MEMOBJTYPE_MAPPING:
111 case RTR0MEMOBJTYPE_PHYS:
112 case RTR0MEMOBJTYPE_PHYS_NC:
113 {
114 if (pMemHaiku->AreaId > -1)
115 rc = delete_area(pMemHaiku->AreaId);
116
117 AssertMsg(rc == B_OK, ("%#x", rc));
118 break;
119 }
120
121 case RTR0MEMOBJTYPE_LOCK:
122 {
123 team_id team = B_SYSTEM_TEAM;
124
125 if (pMemHaiku->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
126 team = ((team_id)pMemHaiku->Core.u.Lock.R0Process);
127
128 rc = unlock_memory_etc(team, pMemHaiku->Core.pv, pMemHaiku->Core.cb, B_READ_DEVICE);
129 AssertMsg(rc == B_OK, ("%#x", rc));
130 break;
131 }
132
133 case RTR0MEMOBJTYPE_RES_VIRT:
134 {
135 team_id team = B_SYSTEM_TEAM;
136 if (pMemHaiku->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
137 team = ((team_id)pMemHaiku->Core.u.Lock.R0Process);
138
139 rc = vm_unreserve_address_range(team, pMemHaiku->Core.pv, pMemHaiku->Core.cb);
140 AssertMsg(rc == B_OK, ("%#x", rc));
141 break;
142 }
143
144 default:
145 AssertMsgFailed(("enmType=%d\n", pMemHaiku->Core.enmType));
146 return VERR_INTERNAL_ERROR;
147 }
148
149 return VINF_SUCCESS;
150}
151
152
153static int rtR0MemObjNativeAllocArea(PPRTR0MEMOBJINTERNAL ppMem, size_t cb,
154 bool fExecutable, RTR0MEMOBJTYPE type, RTHCPHYS PhysHighest, size_t uAlignment)
155{
156 NOREF(fExecutable);
157
158 int rc;
159 void *pvMap = NULL;
160 const char *pszName = NULL;
161 uint32 addressSpec = B_ANY_KERNEL_ADDRESS;
162 uint32 fLock = ~0U;
163 LogFlowFunc(("ppMem=%p cb=%u, fExecutable=%s, type=%08x, PhysHighest=%RX64 uAlignment=%u\n", ppMem,(unsigned)cb,
164 fExecutable ? "true" : "false", type, PhysHighest,(unsigned)uAlignment));
165
166 switch (type)
167 {
168 case RTR0MEMOBJTYPE_PAGE:
169 pszName = "IPRT R0MemObj Alloc";
170 fLock = B_FULL_LOCK;
171 break;
172 case RTR0MEMOBJTYPE_LOW:
173 pszName = "IPRT R0MemObj AllocLow";
174 fLock = B_32_BIT_FULL_LOCK;
175 break;
176 case RTR0MEMOBJTYPE_CONT:
177 pszName = "IPRT R0MemObj AllocCont";
178 fLock = B_32_BIT_CONTIGUOUS;
179 break;
180#if 0
181 case RTR0MEMOBJTYPE_MAPPING:
182 pszName = "IPRT R0MemObj Mapping";
183 fLock = B_FULL_LOCK;
184 break;
185#endif
186 case RTR0MEMOBJTYPE_PHYS:
187 /** @todo alignment */
188 if (uAlignment != PAGE_SIZE)
189 return VERR_NOT_SUPPORTED;
190 /** @todo r=ramshankar: no 'break' here?? */
191 case RTR0MEMOBJTYPE_PHYS_NC:
192 pszName = "IPRT R0MemObj AllocPhys";
193 fLock = (PhysHighest < _4G ? B_LOMEM : B_32_BIT_CONTIGUOUS);
194 break;
195#if 0
196 case RTR0MEMOBJTYPE_LOCK:
197 break;
198#endif
199 default:
200 return VERR_INTERNAL_ERROR;
201 }
202
203 /* Create the object. */
204 PRTR0MEMOBJHAIKU pMemHaiku;
205 pMemHaiku = (PRTR0MEMOBJHAIKU)rtR0MemObjNew(sizeof(RTR0MEMOBJHAIKU), type, NULL, cb);
206 if (RT_UNLIKELY(!pMemHaiku))
207 return VERR_NO_MEMORY;
208
209 rc = pMemHaiku->AreaId = create_area(pszName, &pvMap, addressSpec, cb, fLock, B_READ_AREA | B_WRITE_AREA);
210 if (pMemHaiku->AreaId >= 0)
211 {
212 physical_entry physMap[2];
213 pMemHaiku->Core.pv = pvMap; /* store start address */
214 switch (type)
215 {
216 case RTR0MEMOBJTYPE_CONT:
217 rc = get_memory_map(pvMap, cb, physMap, 2);
218 if (rc == B_OK)
219 pMemHaiku->Core.u.Cont.Phys = physMap[0].address;
220 break;
221
222 case RTR0MEMOBJTYPE_PHYS:
223 case RTR0MEMOBJTYPE_PHYS_NC:
224 rc = get_memory_map(pvMap, cb, physMap, 2);
225 if (rc == B_OK)
226 {
227 pMemHaiku->Core.u.Phys.PhysBase = physMap[0].address;
228 pMemHaiku->Core.u.Phys.fAllocated = true;
229 }
230 break;
231
232 default:
233 break;
234 }
235 if (rc >= B_OK)
236 {
237 *ppMem = &pMemHaiku->Core;
238 return VINF_SUCCESS;
239 }
240
241 delete_area(pMemHaiku->AreaId);
242 }
243
244 rtR0MemObjDelete(&pMemHaiku->Core);
245 return RTErrConvertFromHaikuKernReturn(rc);
246}
247
248
249int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
250{
251 return rtR0MemObjNativeAllocArea(ppMem, cb, fExecutable, RTR0MEMOBJTYPE_PAGE, 0 /* PhysHighest */, 0 /* uAlignment */);
252}
253
254
255int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
256{
257 return rtR0MemObjNativeAllocArea(ppMem, cb, fExecutable, RTR0MEMOBJTYPE_LOW, 0 /* PhysHighest */, 0 /* uAlignment */);
258}
259
260
261int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
262{
263 return rtR0MemObjNativeAllocArea(ppMem, cb, fExecutable, RTR0MEMOBJTYPE_CONT, 0 /* PhysHighest */, 0 /* uAlignment */);
264}
265
266int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
267{
268 return rtR0MemObjNativeAllocArea(ppMem, cb, false, RTR0MEMOBJTYPE_PHYS, PhysHighest, uAlignment);
269}
270
271
272int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
273{
274 return rtR0MemObjNativeAllocPhys(ppMem, cb, PhysHighest, PAGE_SIZE);
275}
276
277
278int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
279{
280 AssertReturn(uCachePolicy == RTMEM_CACHE_POLICY_DONT_CARE, VERR_NOT_SUPPORTED);
281 LogFlowFunc(("ppMem=%p Phys=%08x cb=%u uCachePolicy=%x\n", ppMem, Phys,(unsigned)cb, uCachePolicy));
282
283 /* Create the object. */
284 PRTR0MEMOBJHAIKU pMemHaiku = (PRTR0MEMOBJHAIKU)rtR0MemObjNew(sizeof(*pMemHaiku), RTR0MEMOBJTYPE_PHYS, NULL, cb);
285 if (!pMemHaiku)
286 return VERR_NO_MEMORY;
287
288 /* There is no allocation here, it needs to be mapped somewhere first. */
289 pMemHaiku->AreaId = -1;
290 pMemHaiku->Core.u.Phys.fAllocated = false;
291 pMemHaiku->Core.u.Phys.PhysBase = Phys;
292 pMemHaiku->Core.u.Phys.uCachePolicy = uCachePolicy;
293 *ppMem = &pMemHaiku->Core;
294 return VINF_SUCCESS;
295}
296
297
298/**
299 * Worker locking the memory in either kernel or user maps.
300 *
301 * @returns IPRT status code.
302 * @param ppMem Where to store the allocated memory object.
303 * @param pvStart The starting address.
304 * @param cb The size of the block.
305 * @param fAccess The mapping protection to apply.
306 * @param R0Process The process to map the memory to (use NIL_RTR0PROCESS
307 * for the kernel)
308 * @param fFlags Memory flags (B_READ_DEVICE indicates the memory is
309 * intended to be written from a "device").
310 */
311static int rtR0MemObjNativeLockInMap(PPRTR0MEMOBJINTERNAL ppMem, void *pvStart, size_t cb, uint32_t fAccess,
312 RTR0PROCESS R0Process, int fFlags)
313{
314 NOREF(fAccess);
315 int rc;
316 team_id TeamId = B_SYSTEM_TEAM;
317
318 LogFlowFunc(("ppMem=%p pvStart=%p cb=%u fAccess=%x R0Process=%d fFlags=%x\n", ppMem, pvStart, cb, fAccess, R0Process,
319 fFlags));
320
321 /* Create the object. */
322 PRTR0MEMOBJHAIKU pMemHaiku = (PRTR0MEMOBJHAIKU)rtR0MemObjNew(sizeof(*pMemHaiku), RTR0MEMOBJTYPE_LOCK, pvStart, cb);
323 if (RT_UNLIKELY(!pMemHaiku))
324 return VERR_NO_MEMORY;
325
326 if (R0Process != NIL_RTR0PROCESS)
327 TeamId = (team_id)R0Process;
328 rc = lock_memory_etc(TeamId, pvStart, cb, fFlags);
329 if (rc == B_OK)
330 {
331 pMemHaiku->AreaId = -1;
332 pMemHaiku->Core.u.Lock.R0Process = R0Process;
333 *ppMem = &pMemHaiku->Core;
334 return VINF_SUCCESS;
335 }
336 rtR0MemObjDelete(&pMemHaiku->Core);
337 return RTErrConvertFromHaikuKernReturn(rc);
338}
339
340
341int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
342{
343 return rtR0MemObjNativeLockInMap(ppMem, (void *)R3Ptr, cb, fAccess, R0Process, B_READ_DEVICE);
344}
345
346
347int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
348{
349 return rtR0MemObjNativeLockInMap(ppMem, pv, cb, fAccess, NIL_RTR0PROCESS, B_READ_DEVICE);
350}
351
352
353#if 0
354/** @todo Reserve address space */
355/**
356 * Worker for the two virtual address space reservers.
357 *
358 * We're leaning on the examples provided by mmap and vm_mmap in vm_mmap.c here.
359 */
360static int rtR0MemObjNativeReserveInMap(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment,
361 RTR0PROCESS R0Process)
362{
363 int rc;
364 team_id TeamId = B_SYSTEM_TEAM;
365
366 LogFlowFunc(("ppMem=%p pvFixed=%p cb=%u uAlignment=%u R0Process=%d\n", ppMem, pvFixed, (unsigned)cb, uAlignment, R0Process));
367
368 if (R0Process != NIL_RTR0PROCESS)
369 team = (team_id)R0Process;
370
371 /* Check that the specified alignment is supported. */
372 if (uAlignment > PAGE_SIZE)
373 return VERR_NOT_SUPPORTED;
374
375 /* Create the object. */
376 PRTR0MEMOBJHAIKU pMemHaiku = (PRTR0MEMOBJHAIKU)rtR0MemObjNew(sizeof(*pMemHaiku), RTR0MEMOBJTYPE_RES_VIRT, NULL, cb);
377 if (!pMemHaiku)
378 return VERR_NO_MEMORY;
379
380 /* Ask the kernel to reserve the address range. */
381 //XXX: vm_reserve_address_range ?
382 return VERR_NOT_SUPPORTED;
383}
384#endif
385
386
387int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
388{
389 return VERR_NOT_SUPPORTED;
390}
391
392
393int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
394{
395 return VERR_NOT_SUPPORTED;
396}
397
398
399int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
400 unsigned fProt, size_t offSub, size_t cbSub)
401{
402 PRTR0MEMOBJHAIKU pMemToMapHaiku = (PRTR0MEMOBJHAIKU)pMemToMap;
403 PRTR0MEMOBJHAIKU pMemHaiku;
404 area_id area = -1;
405 void *pvMap = pvFixed;
406 uint32 uAddrSpec = B_EXACT_ADDRESS;
407 uint32 fProtect = 0;
408 int rc = VERR_MAP_FAILED;
409 AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
410 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
411#if 0
412 /** @todo r=ramshankar: Wrong format specifiers, fix later! */
413 dprintf("%s(%p, %p, %p, %d, %x, %u, %u)\n", __FUNCTION__, ppMem, pMemToMap, pvFixed, uAlignment,
414 fProt, offSub, cbSub);
415#endif
416 /* Check that the specified alignment is supported. */
417 if (uAlignment > PAGE_SIZE)
418 return VERR_NOT_SUPPORTED;
419
420 /* We can't map anything to the first page, sorry. */
421 if (pvFixed == 0)
422 return VERR_NOT_SUPPORTED;
423
424 if (fProt & RTMEM_PROT_READ)
425 fProtect |= B_KERNEL_READ_AREA;
426 if (fProt & RTMEM_PROT_WRITE)
427 fProtect |= B_KERNEL_WRITE_AREA;
428
429 /*
430 * Either the object we map has an area associated with, which we can clone,
431 * or it's a physical address range which we must map.
432 */
433 if (pMemToMapHaiku->AreaId > -1)
434 {
435 if (pvFixed == (void *)-1)
436 uAddrSpec = B_ANY_KERNEL_ADDRESS;
437
438 rc = area = clone_area("IPRT R0MemObj MapKernel", &pvMap, uAddrSpec, fProtect, pMemToMapHaiku->AreaId);
439 LogFlow(("rtR0MemObjNativeMapKernel: clone_area uAddrSpec=%d fProtect=%x AreaId=%d rc=%d\n", uAddrSpec, fProtect,
440 pMemToMapHaiku->AreaId, rc));
441 }
442 else if (pMemToMapHaiku->Core.enmType == RTR0MEMOBJTYPE_PHYS)
443 {
444 /* map_physical_memory() won't let you choose where. */
445 if (pvFixed != (void *)-1)
446 return VERR_NOT_SUPPORTED;
447 uAddrSpec = B_ANY_KERNEL_ADDRESS;
448
449 rc = area = map_physical_memory("IPRT R0MemObj MapKernelPhys", (phys_addr_t)pMemToMapHaiku->Core.u.Phys.PhysBase,
450 pMemToMapHaiku->Core.cb, uAddrSpec, fProtect, &pvMap);
451 }
452 else
453 return VERR_NOT_SUPPORTED;
454
455 if (rc >= B_OK)
456 {
457 /* Create the object. */
458 pMemHaiku = (PRTR0MEMOBJHAIKU)rtR0MemObjNew(sizeof(RTR0MEMOBJHAIKU), RTR0MEMOBJTYPE_MAPPING, pvMap,
459 pMemToMapHaiku->Core.cb);
460 if (RT_UNLIKELY(!pMemHaiku))
461 return VERR_NO_MEMORY;
462
463 pMemHaiku->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
464 pMemHaiku->Core.pv = pvMap;
465 pMemHaiku->AreaId = area;
466 *ppMem = &pMemHaiku->Core;
467 return VINF_SUCCESS;
468 }
469 rc = VERR_MAP_FAILED;
470
471 /** @todo finish the implementation. */
472
473 rtR0MemObjDelete(&pMemHaiku->Core);
474 return rc;
475}
476
477
478int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment,
479 unsigned fProt, RTR0PROCESS R0Process)
480{
481#if 0
482 /*
483 * Check for unsupported stuff.
484 */
485 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
486 AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
487 if (uAlignment > PAGE_SIZE)
488 return VERR_NOT_SUPPORTED;
489
490 int rc;
491 PRTR0MEMOBJHAIKU pMemToMapHaiku = (PRTR0MEMOBJHAIKU)pMemToMap;
492 struct proc *pProc = (struct proc *)R0Process;
493 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
494
495 /* calc protection */
496 vm_prot_t ProtectionFlags = 0;
497 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
498 ProtectionFlags = VM_PROT_NONE;
499 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
500 ProtectionFlags |= VM_PROT_READ;
501 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
502 ProtectionFlags |= VM_PROT_WRITE;
503 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
504 ProtectionFlags |= VM_PROT_EXECUTE;
505
506 /* calc mapping address */
507 PROC_LOCK(pProc);
508 vm_offset_t AddrR3 = round_page((vm_offset_t)pProc->p_vmspace->vm_daddr + lim_max(pProc, RLIMIT_DATA));
509 PROC_UNLOCK(pProc);
510
511 /* Insert the object in the map. */
512 rc = vm_map_find(pProcMap, /* Map to insert the object in */
513 NULL, /* Object to map */
514 0, /* Start offset in the object */
515 &AddrR3, /* Start address IN/OUT */
516 pMemToMap->cb, /* Size of the mapping */
517 TRUE, /* Whether a suitable address should be searched for first */
518 ProtectionFlags, /* protection flags */
519 VM_PROT_ALL, /* Maximum protection flags */
520 0); /* Copy on write */
521
522 /* Map the memory page by page into the destination map. */
523 if (rc == KERN_SUCCESS)
524 {
525 size_t cPages = pMemToMap->cb >> PAGE_SHIFT;;
526 pmap_t pPhysicalMap = pProcMap->pmap;
527 vm_offset_t AddrR3Dst = AddrR3;
528
529 if ( pMemToMap->enmType == RTR0MEMOBJTYPE_PHYS
530 || pMemToMap->enmType == RTR0MEMOBJTYPE_PHYS_NC
531 || pMemToMap->enmType == RTR0MEMOBJTYPE_PAGE)
532 {
533 /* Mapping physical allocations */
534 Assert(cPages == pMemToMapHaiku->u.Phys.cPages);
535
536 /* Insert the memory page by page into the mapping. */
537 for (uint32_t iPage = 0; iPage < cPages; iPage++)
538 {
539 vm_page_t pPage = pMemToMapHaiku->u.Phys.apPages[iPage];
540
541 MY_PMAP_ENTER(pPhysicalMap, AddrR3Dst, pPage, ProtectionFlags, TRUE);
542 AddrR3Dst += PAGE_SIZE;
543 }
544 }
545 else
546 {
547 /* Mapping cont or low memory types */
548 vm_offset_t AddrToMap = (vm_offset_t)pMemToMap->pv;
549
550 for (uint32_t iPage = 0; iPage < cPages; iPage++)
551 {
552 vm_page_t pPage = PHYS_TO_VM_PAGE(vtophys(AddrToMap));
553
554 MY_PMAP_ENTER(pPhysicalMap, AddrR3Dst, pPage, ProtectionFlags, TRUE);
555 AddrR3Dst += PAGE_SIZE;
556 AddrToMap += PAGE_SIZE;
557 }
558 }
559 }
560
561 if (RT_SUCCESS(rc))
562 {
563 /*
564 * Create a mapping object for it.
565 */
566 PRTR0MEMOBJHAIKU pMemHaiku = (PRTR0MEMOBJHAIKU)rtR0MemObjNew(sizeof(RTR0MEMOBJHAIKU),
567 RTR0MEMOBJTYPE_MAPPING,
568 (void *)AddrR3,
569 pMemToMap->cb);
570 if (pMemHaiku)
571 {
572 Assert((vm_offset_t)pMemHaiku->Core.pv == AddrR3);
573 pMemHaiku->Core.u.Mapping.R0Process = R0Process;
574 *ppMem = &pMemHaiku->Core;
575 return VINF_SUCCESS;
576 }
577
578 rc = vm_map_remove(pProcMap, ((vm_offset_t)AddrR3), ((vm_offset_t)AddrR3) + pMemToMap->cb);
579 AssertMsg(rc == KERN_SUCCESS, ("Deleting mapping failed\n"));
580 }
581#endif
582 return VERR_NOT_SUPPORTED;
583}
584
585
586int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
587{
588 return VERR_NOT_SUPPORTED;
589}
590
591
592RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
593{
594 PRTR0MEMOBJHAIKU pMemHaiku = (PRTR0MEMOBJHAIKU)pMem;
595 status_t rc;
596
597 /** @todo r=ramshankar: Validate objects */
598
599 LogFlow(("rtR0MemObjNativeGetPagePhysAddr: pMem=%p enmType=%x iPage=%u\n", pMem, pMemHaiku->Core.enmType,(unsigned)iPage));
600
601 switch (pMemHaiku->Core.enmType)
602 {
603 case RTR0MEMOBJTYPE_LOCK:
604 {
605 team_id TeamId = B_SYSTEM_TEAM;
606 physical_entry aPhysMap[2];
607 int32 cPhysMap = 2; /** @todo r=ramshankar: why not use RT_ELEMENTS? */
608
609 if (pMemHaiku->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
610 TeamId = (team_id)pMemHaiku->Core.u.Lock.R0Process;
611 void *pb = pMemHaiku->Core.pv + (iPage << PAGE_SHIFT);
612
613 rc = get_memory_map_etc(TeamId, pb, B_PAGE_SIZE, aPhysMap, &cPhysMap);
614 if (rc < B_OK || cPhysMap < 1)
615 return NIL_RTHCPHYS;
616
617 return aPhysMap[0].address;
618 }
619
620#if 0
621 case RTR0MEMOBJTYPE_MAPPING:
622 {
623 vm_offset_t pb = (vm_offset_t)pMemHaiku->Core.pv + (iPage << PAGE_SHIFT);
624
625 if (pMemHaiku->Core.u.Mapping.R0Process != NIL_RTR0PROCESS)
626 {
627 struct proc *pProc = (struct proc *)pMemHaiku->Core.u.Mapping.R0Process;
628 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
629 pmap_t pPhysicalMap = pProcMap->pmap;
630
631 return pmap_extract(pPhysicalMap, pb);
632 }
633 return vtophys(pb);
634 }
635#endif
636 case RTR0MEMOBJTYPE_CONT:
637 return pMemHaiku->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
638
639 case RTR0MEMOBJTYPE_PHYS:
640 return pMemHaiku->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
641
642 case RTR0MEMOBJTYPE_LOW:
643 case RTR0MEMOBJTYPE_PAGE:
644 case RTR0MEMOBJTYPE_PHYS_NC:
645 {
646 team_id TeamId = B_SYSTEM_TEAM;
647 physical_entry aPhysMap[2];
648 int32 cPhysMap = 2; /** @todo r=ramshankar: why not use RT_ELEMENTS? */
649
650 void *pb = pMemHaiku->Core.pv + (iPage << PAGE_SHIFT);
651 rc = get_memory_map_etc(TeamId, pb, B_PAGE_SIZE, aPhysMap, &cPhysMap);
652 if (rc < B_OK || cPhysMap < 1)
653 return NIL_RTHCPHYS;
654
655 return aPhysMap[0].address;
656 }
657
658 case RTR0MEMOBJTYPE_RES_VIRT:
659 default:
660 return NIL_RTHCPHYS;
661 }
662}
663
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