VirtualBox

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

Last change on this file since 91478 was 91478, checked in by vboxsync, 3 years ago

IPRT/memobj: Added pszTag to rtR0MemObjNew.

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