memobj-r0drv-nt.cpp revision e5f6ad85110ebdc7256808a0a3a8ce330370be6a
/* $Id$ */
/** @file
* innotek Portable Runtime - Ring-0 Memory Objects, NT.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "the-nt-kernel.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Maximum number of bytes we try to lock down in one go.
* This is supposed to have a limit right below 256MB, but this appears
* to actually be much lower. The values here have been determined experimentally.
*/
#ifdef RT_ARCH_X86
#endif
#ifdef RT_ARCH_AMD64
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The NT version of the memory object structure.
*/
typedef struct RTR0MEMOBJNT
{
/** The core structure. */
#ifndef IPRT_TARGET_NT4
/** Used MmAllocatePagesForMdl(). */
bool fAllocatedPagesForMdl;
#endif
/** The number of PMDLs (memory descriptor lists) in the array. */
unsigned cMdls;
/** Array of MDL pointers. (variable size) */
{
/*
* Deal with it on a per type basis (just as a variation).
*/
{
case RTR0MEMOBJTYPE_LOW:
#ifdef IPRT_TARGET_NT4
if (pMemNt->fAllocatedPagesForMdl)
{
break;
}
#endif
/* fall thru */
case RTR0MEMOBJTYPE_PAGE:
break;
case RTR0MEMOBJTYPE_CONT:
break;
case RTR0MEMOBJTYPE_PHYS:
case RTR0MEMOBJTYPE_PHYS_NC:
#ifdef IPRT_TARGET_NT4
if (pMemNt->fAllocatedPagesForMdl)
{
}
#endif
break;
case RTR0MEMOBJTYPE_LOCK:
{
}
break;
case RTR0MEMOBJTYPE_RES_VIRT:
{
}
else
{
}
AssertMsgFailed(("RTR0MEMOBJTYPE_RES_VIRT\n"));
return VERR_INTERNAL_ERROR;
break;
case RTR0MEMOBJTYPE_MAPPING:
{
break;
}
default:
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
{
/*
* Try allocate the memory and create an MDL for them so
* we can query the physical addresses and do mappings later
* without running into out-of-memory conditions and similar problems.
*/
int rc = VERR_NO_PAGE_MEMORY;
if (pv)
{
if (pMdl)
{
/** @todo if (fExecutable) */
/*
* Create the IPRT memory object.
*/
if (pMemNt)
{
return VINF_SUCCESS;
}
rc = VERR_NO_MEMORY;
}
ExFreePool(pv);
}
return rc;
}
{
/*
* Try see if we get lucky first...
* (We could probably just assume we're lucky on NT4.)
*/
if (RT_SUCCESS(rc))
{
while (iPage-- > 0)
{
rc = VERR_NO_MEMORY;
break;
}
if (RT_SUCCESS(rc))
return rc;
/* The following ASSUMES that rtR0MemObjNativeAllocPage returns a completed object. */
RTR0MemObjFree(*ppMem, false);
}
#ifndef IPRT_TARGET_NT4
/*
* Use MmAllocatePagesForMdl to specify the range of physical addresses we wish to use.
*/
if (pMdl)
{
{
{
if (pv)
{
if (pMemNt)
{
pMemNt->fAllocatedPagesForMdl = true;
return VINF_SUCCESS;
}
}
}
{
/* nothing */
}
}
}
#endif /* !IPRT_TARGET_NT4 */
/*
* Fall back on contiguous memory...
*/
}
/**
* Internal worker for rtR0MemObjNativeAllocCont(), rtR0MemObjNativeAllocPhys()
* and rtR0MemObjNativeAllocPhysNC() that takes a max physical address in addition
* to what rtR0MemObjNativeAllocCont() does.
*
* @returns IPRT status code.
* @param ppMem Where to store the pointer to the ring-0 memory object.
* @param cb The size.
* @param fExecutable Whether the mapping should be executable or not.
* @param PhysHighest The highest physical address for the pages in allocation.
*/
static int rtR0MemObjNativeAllocContEx(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable, RTHCPHYS PhysHighest)
{
/*
* Allocate the memory and create an MDL for it.
*/
if (!pv)
return VERR_NO_MEMORY;
if (pMdl)
{
/** @todo fExecutable */
if (pMemNt)
{
return VINF_SUCCESS;
}
}
return VERR_NO_MEMORY;
}
{
}
{
#ifndef IPRT_TARGET_NT4
/*
* Try and see if we're lucky and get a contiguous chunk from MmAllocatePagesForMdl.
*
* If the allocation is big, the chances are *probably* not very good. The current
* max limit is kind of random.
*/
{
if (pMdl)
{
{
break;
{
PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)rtR0MemObjNew(sizeof(*pMemNt), RTR0MEMOBJTYPE_PHYS, NULL, cb);
if (pMemNt)
{
pMemNt->fAllocatedPagesForMdl = true;
return VINF_SUCCESS;
}
}
}
}
}
#endif
}
{
#ifndef IPRT_TARGET_NT4
if (pMdl)
{
{
if (pMemNt)
{
pMemNt->fAllocatedPagesForMdl = true;
return VINF_SUCCESS;
}
}
}
return VERR_NO_MEMORY;
#else /* IPRT_TARGET_NT4 */
return VERR_NOT_SUPPORTED;
#endif /* IPRT_TARGET_NT4 */
}
{
/*
* Validate the address range and create a descriptor for it.
*/
return VERR_ADDRESS_TOO_BIG;
/*
* Create the IPRT memory object.
*/
PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)rtR0MemObjNew(sizeof(*pMemNt), RTR0MEMOBJTYPE_PHYS, NULL, cb);
if (pMemNt)
{
return VINF_SUCCESS;
}
return VERR_NO_MEMORY;
}
/**
* 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.
*/
{
/*
* Calc the number of MDLs we need and allocate the memory object structure.
*/
cMdls++;
if (!pMemNt)
return VERR_NO_MEMORY;
/*
* Loop locking down the sub parts of the memory.
*/
int rc = VINF_SUCCESS;
unsigned iMdl;
{
/*
* Calc the Mdl size and allocate it.
*/
if (cbCur > MAX_LOCK_MEM_SIZE)
if (!pMdl)
{
rc = VERR_NO_MEMORY;
break;
}
/*
* Lock the pages.
*/
{
}
{
break;
}
/* next */
}
if (RT_SUCCESS(rc))
{
return rc;
}
/*
* We failed, perform cleanups.
*/
while (iMdl-- > 0)
{
}
return SUPDRV_ERR_LOCK_FAILED;
}
int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
{
AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
/* ( Can use MmProbeAndLockProcessPages if we need to mess with other processes later.) */
}
{
}
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.
*/
if (pMemNt)
{
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.
*/
if (pMemNt)
{
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)pMemNt->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=%VHp Addr=%RX64\n", PhysAddr, (uint64_t)Addr), NIL_RTHCPHYS);
}
return PhysAddr;
}