memobj-r0drv-darwin.cpp revision cca8c8c55206ccd60f1b32843a67ce737447ac60
/* $Id$ */
/** @file
* IPRT - Ring-0 Memory Objects, Darwin.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "the-darwin-kernel.h"
#define USE_VM_MAP_WIRE
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The Darwin version of the memory object structure.
*/
typedef struct RTR0MEMOBJDARWIN
{
/** The core structure. */
/** Pointer to the memory descriptor created for allocated and locked memory. */
/** Pointer to the memory mapping object for mapped memory. */
{
/*
* Release the IOMemoryDescriptor/IOMemoryMap associated with the object.
*/
if (pMemDarwin->pMemDesc)
{
}
else if (pMemDarwin->pMemMap)
{
}
/*
* Release any memory that we've allocated or locked.
*/
{
case RTR0MEMOBJTYPE_LOW:
case RTR0MEMOBJTYPE_PAGE:
break;
case RTR0MEMOBJTYPE_CONT:
break;
case RTR0MEMOBJTYPE_LOCK:
{
#ifdef USE_VM_MAP_WIRE
: kernel_map;
0 /* not user */);
#endif
break;
}
case RTR0MEMOBJTYPE_PHYS:
/*if (pMemDarwin->Core.u.Phys.fAllocated)
IOFreePhysical(pMemDarwin->Core.u.Phys.PhysBase, pMemDarwin->Core.cb);*/
break;
case RTR0MEMOBJTYPE_PHYS_NC:
AssertMsgFailed(("RTR0MEMOBJTYPE_PHYS_NC\n"));
return VERR_INTERNAL_ERROR;
break;
case RTR0MEMOBJTYPE_RES_VIRT:
AssertMsgFailed(("RTR0MEMOBJTYPE_RES_VIRT\n"));
return VERR_INTERNAL_ERROR;
break;
case RTR0MEMOBJTYPE_MAPPING:
/* nothing to do here. */
break;
default:
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
{
/*
* Try allocate the memory and create it's IOMemoryDescriptor first.
*/
int rc = VERR_NO_PAGE_MEMORY;
if (pv)
{
IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddress((vm_address_t)pv, cb, kIODirectionInOut, kernel_task);
if (pMemDesc)
{
/*
* Create the IPRT memory object.
*/
PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_PAGE, pv, cb);
if (pMemDarwin)
{
return VINF_SUCCESS;
}
rc = VERR_NO_MEMORY;
}
else
}
return rc;
}
{
#if 1
/*
* Allocating 128KB continguous memory for the low page pool can bit a bit
* exhausting on the kernel, it frequently causes the entire box to lock
* up on startup.
*
* So, try allocate the memory using IOMallocAligned first and if we get any high
* physical memory we'll release it and fall back on IOMAllocContiguous.
*/
int rc = VERR_NO_PAGE_MEMORY;
if (pv)
{
IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddress((vm_address_t)pv, cb, kIODirectionInOut, kernel_task);
if (pMemDesc)
{
/*
* Check if it's all below 4GB.
*/
{
{
/* Ok, we failed, fall back on contiguous allocation. */
}
}
/*
* Create the IPRT memory object.
*/
PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOW, pv, cb);
if (pMemDarwin)
{
return VINF_SUCCESS;
}
rc = VERR_NO_MEMORY;
}
else
}
return rc;
#else
/*
* IOMallocContiguous is the most suitable API.
*/
#endif
}
{
/*
* Try allocate the memory and create it's IOMemoryDescriptor first.
*/
int rc = VERR_NO_CONT_MEMORY;
/// @todo
// Use IOBufferMemoryDescriptor::inTaskWithPhysicalMask(kernel_task, kIOMemoryKernelUserShared | kIODirectionInOut,
// cb, (_4G - 1) ^ PAGE_OFFSET_MASK);
#if 1 /* seems to work fine for cb == PAGE_SIZE, the other variant doesn't. */
#else
#endif
if (pv)
{
IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddress((vm_address_t)pv, cb, kIODirectionInOut, kernel_task);
if (pMemDesc)
{
/* a bit of useful paranoia. */
if ( PhysAddr > 0
{
/*
* Create the IPRT memory object.
*/
PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_CONT, pv, cb);
if (pMemDarwin)
{
return VINF_SUCCESS;
}
rc = VERR_NO_MEMORY;
}
else
{
}
}
else
}
/*
* Workaround for odd IOMallocContiguous behavior, just in case.
*/
return rc;
}
{
#if 0 /* turned out IOMallocPhysical isn't exported yet. sigh. */
/*
* Try allocate the memory and create it's IOMemoryDescriptor first.
* Note that IOMallocPhysical is not working correctly (it's ignoring the mask).
*/
/* first calc the mask (in the hope that it'll be used) */
if (PhysHighest != NIL_RTHCPHYS)
{
PhysMask = ~(IOPhysicalAddress)0;
while (PhysMask > PhysHighest)
PhysMask >>= 1;
}
/* try allocate physical memory. */
int rc = VERR_NO_PHYS_MEMORY;
if (PhysAddr64)
{
if ( PhysAddr == PhysAddr64
&& PhysAddr < PhysHighest
{
/* create a descriptor. */
IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withPhysicalAddress(PhysAddr, cb, kIODirectionInOut);
if (pMemDesc)
{
/*
* Create the IPRT memory object.
*/
PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_PHYS, NULL, cb);
if (pMemDarwin)
{
return VINF_SUCCESS;
}
rc = VERR_NO_MEMORY;
}
else
}
else
{
AssertMsgFailed(("PhysAddr=%#llx PhysAddr64=%#llx PhysHigest=%#llx\n", (unsigned long long)PhysAddr,
(unsigned long long)PhysAddr64, (unsigned long long)PhysHighest));
}
}
/*
* Just in case IOMallocContiguous doesn't work right, we can try fall back
* on a contiguous allcation.
*/
{
if (RT_SUCCESS(rc2))
}
return rc;
#else
#endif
}
{
/** @todo rtR0MemObjNativeAllocPhys / darwin.
* This might be a bit problematic and may very well require having to create our own
* object which we populate with pages but without mapping it into any address space.
* Estimate is 2-3 days.
*/
return VERR_NOT_SUPPORTED;
}
{
/*
* Validate the address range and create a descriptor for it.
*/
int rc = VERR_ADDRESS_TOO_BIG;
{
IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withPhysicalAddress(PhysAddr, cb, kIODirectionInOut);
if (pMemDesc)
{
/*
* Create the IPRT memory object.
*/
PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_PHYS, NULL, cb);
if (pMemDarwin)
{
return VINF_SUCCESS;
}
rc = VERR_NO_MEMORY;
}
}
else
return rc;
}
/**
* Internal worker for locking down pages.
*
* @return IPRT status code.
*
* @param ppMem Where to store the memory object pointer.
* @param pv First page.
* @param cb Number of bytes.
* @param Task The task \a pv and \a cb refers to.
*/
{
#ifdef USE_VM_MAP_WIRE
/*
* First try lock the memory.
*/
int rc = VERR_LOCK_FAILED;
0 /* not user */);
if (kr == KERN_SUCCESS)
{
/*
* Create the IPRT memory object.
*/
PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOCK, pv, cb);
if (pMemDarwin)
{
return VINF_SUCCESS;
}
kr = vm_map_unwire(get_task_map(Task), (vm_map_offset_t)pv, (vm_map_offset_t)pv + cb, 0 /* not user */);
rc = VERR_NO_MEMORY;
}
#else
/*
* Create a descriptor and try lock it (prepare).
*/
int rc = VERR_MEMOBJ_INIT_FAILED;
IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddress((vm_address_t)pv, cb, kIODirectionInOut, Task);
if (pMemDesc)
{
if (IORet == kIOReturnSuccess)
{
/*
* Create the IPRT memory object.
*/
PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOCK, pv, cb);
if (pMemDarwin)
{
return VINF_SUCCESS;
}
rc = VERR_NO_MEMORY;
}
else
}
#endif
return rc;
}
int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
{
}
{
}
int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
{
return VERR_NOT_IMPLEMENTED;
}
int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
{
return VERR_NOT_IMPLEMENTED;
}
int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment, unsigned fProt)
{
/*
* Must have a memory descriptor.
*/
int rc = VERR_INVALID_PARAMETER;
if (pMemToMapDarwin->pMemDesc)
{
if (pMemMap)
{
{
/*
* Create the IPRT memory object.
*/
PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_MAPPING,
if (pMemDarwin)
{
return VINF_SUCCESS;
}
rc = VERR_NO_MEMORY;
}
else
}
else
}
return rc;
}
int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
{
/*
* Must have a memory descriptor.
*/
int rc = VERR_INVALID_PARAMETER;
if (pMemToMapDarwin->pMemDesc)
{
if (pMemMap)
{
{
/*
* Create the IPRT memory object.
*/
PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_MAPPING,
if (pMemDarwin)
{
return VINF_SUCCESS;
}
rc = VERR_NO_MEMORY;
}
else
}
else
}
return rc;
}
{
#ifdef USE_VM_MAP_WIRE
/*
* Locked memory doesn't have a memory descriptor and
* needs to be handled differently.
*/
{
else
{
/*
* From what I can tell, Apple seems to have locked up the all the
* available interfaces that could help us obtain the pmap_t of a task
* or vm_map_t.
* So, we'll have to figure out where in the vm_map_t structure it is
* and read it our selves. ASSUMING that kernel_pmap is pointed to by
* kernel_map->pmap, we scan kernel_map to locate the structure offset.
* Not nice, but it will hopefully do the job in a reliable manner...
*
* (get_task_pmap, get_map_pmap or vm_map_pmap is what we really need btw.)
*/
static int s_offPmap = -1;
{
for (; p < pEnd; p++)
if (*p == kernel_pmap)
{
break;
}
}
pmap_t Pmap = *(pmap_t *)((uintptr_t)get_task_map((task_t)pMemDarwin->Core.u.Lock.R0Process) + s_offPmap);
}
}
else
#endif /* USE_VM_MAP_WIRE */
{
/*
* Get the memory descriptor.
*/
if (!pMemDesc)
/*
* If we've got a memory descriptor, use getPhysicalSegment64().
*/
AssertMsgReturn(PhysAddr == Addr, ("PhysAddr=%RHp Addr=%RX64\n", PhysAddr, (uint64_t)Addr), NIL_RTHCPHYS);
}
return PhysAddr;
}