VirtualBox

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

Last change on this file since 101472 was 100357, checked in by vboxsync, 16 months ago

Runtime/RTR0MemObj*: Add PhysHighest parameter to RTR0MemObjAllocCont to indicate the maximum allowed physical address for an allocation, bugref:10457 [second attempt]

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