memobj-r0drv-freebsd.c revision ee6e48c229ef52aee5e968d956ebd066073811ab
/* $Id$ */
/** @file
* innotek Portable Runtime - Ring-0 Memory Objects, FreeBSD.
*/
/*
* Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "the-freebsd-kernel.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The FreeBSD version of the memory object structure.
*/
typedef struct RTR0MEMOBJFREEBSD
{
/** The core structure. */
#if 0
/** Lock for the ring-3 / ring-0 pinned objectes.
* This member might not be allocated for some object types. */
/** Array of physical pages.
* This array can be 0 in length for some object types. */
#endif
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
{
// int rc;
{
case RTR0MEMOBJTYPE_CONT:
break;
#if 0
case RTR0MEMOBJTYPE_PHYS:
//if (!pMemFreeBSD->Core.pv)
// break;
break
case RTR0MEMOBJTYPE_MAPPING:
//if (pMemFreeBSD->Core.u.Mapping.R0Process == NIL_RTR0PROCESS)
// break;
break;
case RTR0MEMOBJTYPE_PAGE:
case RTR0MEMOBJTYPE_LOW:
//rc = KernVMFree(pMemFreeBSD->Core.pv);
//AssertMsg(!rc, ("rc=%d type=%d pv=%p cb=%#zx\n", rc, pMemFreeBSD->Core.enmType, pMemFreeBSD->Core.pv, pMemFreeBSD->Core.cb));
break;
case RTR0MEMOBJTYPE_LOCK:
//rc = KernVMUnlock(&pMemFreeBSD->Lock);
//AssertMsg(!rc, ("rc=%d\n", rc));
break;
case RTR0MEMOBJTYPE_RES_VIRT:
#endif
default:
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
{
/* malloc or like the linker: http://fxr.watson.org/fxr/source/kern/link_elf.c?v=RELENG62#L701 */
#if 0
/* create the object. */
PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_PAGE, NULL, cb);
if (!pMemFreeBSD)
return VERR_NO_MEMORY;
/* do the allocation. */
if (!rc)
{
if (!rc)
{
return VINF_SUCCESS;
}
}
return RTErrConvertFromOS2(rc);
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
{
/* same as Alloc and hope we're lucky, make sure to verify the physical addresses */
#if 0
/* create the object. */
PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJOS2, aPages[cPages]), RTR0MEMOBJTYPE_LOW, NULL, cb);
if (!pMemFreeBSD)
return VERR_NO_MEMORY;
/* do the allocation. */
if (!rc)
{
if (!rc)
{
return VINF_SUCCESS;
}
}
return RTErrConvertFromOS2(rc);
#endif
return VERR_NOT_IMPLEMENTED;
}
{
/* create the object. */
PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_CONT, NULL, cb);
if (!pMemFreeBSD)
return VERR_NO_MEMORY;
/* do the allocation. */
M_IPRTMOBJ, /* type */
0, /* lowest physical address*/
PAGE_SIZE, /* alignment. */
0); /* boundrary */
{
return VINF_SUCCESS;
}
return VERR_NO_MEMORY;
}
{
#if 0
/* create the object. */
PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJOS2, Lock), RTR0MEMOBJTYPE_PHYS, NULL, cb);
if (!pMemFreeBSD)
return VERR_NO_MEMORY;
/* do the allocation. */
int rc = KernVMAlloc(cb, VMDHA_FIXED | VMDHA_CONTIG | (PhysHighest < _4G ? VMDHA_16M : 0), &pMemFreeBSD->Core.pv, (PPVOID)&ulPhys, NULL);
if (!rc)
{
return VINF_SUCCESS;
}
return RTErrConvertFromOS2(rc);
#endif
return VERR_NOT_IMPLEMENTED;
}
{
/* create the object. */
PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_PHYS, NULL, cb);
if (!pMemFreeBSD)
return VERR_NO_MEMORY;
/* there is no allocation here, it needs to be mapped somewhere first. */
return VINF_SUCCESS;
}
int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, RTR0PROCESS R0Process)
{
/* see vslock and vsunlock */
#if 0
AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
/* create the object. */
PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJOS2, aPages[cPages]), RTR0MEMOBJTYPE_LOCK, pv, cb);
if (!pMemFreeBSD)
return VERR_NO_MEMORY;
/* lock it. */
int rc = KernVMLock(VMDHL_LONG | VMDHL_WRITE, pv, cb, &pMemFreeBSD->Lock, &pMemFreeBSD->aPages[0], &cPagesRet);
if (!rc)
{
return VINF_SUCCESS;
}
return RTErrConvertFromOS2(rc);
#endif
return VERR_NOT_IMPLEMENTED;
}
{
#if 0
/* create the object. */
PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJOS2, aPages[cPages]), RTR0MEMOBJTYPE_LOCK, pv, cb);
if (!pMemFreeBSD)
return VERR_NO_MEMORY;
/* lock it. */
int rc = KernVMLock(VMDHL_LONG | VMDHL_WRITE, pv, cb, &pMemFreeBSD->Lock, &pMemFreeBSD->aPages[0], &cPagesRet);
if (!rc)
{
return VINF_SUCCESS;
}
return RTErrConvertFromOS2(rc);
#endif
return VERR_NOT_IMPLEMENTED;
}
int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
{
/* see kmem_alloc_nofault */
return VERR_NOT_IMPLEMENTED;
}
int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
{
/* see kmem_alloc_nofault */
return VERR_NOT_IMPLEMENTED;
}
int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment, unsigned fProt)
{
/* Phys: see pmap_mapdev in i386/i386/pmap.c (http://fxr.watson.org/fxr/source/i386/i386/pmap.c?v=RELENG62#L2860) */
#if 0
/** @todo finish the implementation. */
int rc;
{
/*
* These has kernel mappings.
*/
case RTR0MEMOBJTYPE_PAGE:
case RTR0MEMOBJTYPE_LOW:
case RTR0MEMOBJTYPE_CONT:
break;
case RTR0MEMOBJTYPE_PHYS:
if (!pvR0)
{
/* no ring-0 mapping, so allocate a mapping in the process. */
if (rc)
return RTErrConvertFromOS2(rc);
}
break;
case RTR0MEMOBJTYPE_LOCK:
return VERR_NOT_SUPPORTED; /** @todo implement this... */
break;
case RTR0MEMOBJTYPE_RES_VIRT:
case RTR0MEMOBJTYPE_MAPPING:
default:
return VERR_INTERNAL_ERROR;
}
/*
* Create a dummy mapping object for it.
*
* any cache options, so sharing is ok. And the main memory object
* isn't actually freed until all the mappings have been freed up
* (reference counting).
*/
PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJOS2, Lock), RTR0MEMOBJTYPE_MAPPING, pvR0, pMemToMapOs2->Core.cb);
if (pMemFreeBSD)
{
return VINF_SUCCESS;
}
return VERR_NO_MEMORY;
#endif
return VERR_NOT_IMPLEMENTED;
}
int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
{
AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
#if 0
int rc;
void *pvR0;
{
/*
* These has kernel mappings.
*/
case RTR0MEMOBJTYPE_PAGE:
case RTR0MEMOBJTYPE_LOW:
case RTR0MEMOBJTYPE_CONT:
break;
case RTR0MEMOBJTYPE_PHYS:
#if 0/* this is wrong. */
if (!pvR0)
{
/* no ring-0 mapping, so allocate a mapping in the process. */
if (rc)
return RTErrConvertFromOS2(rc);
}
break;
#endif
return VERR_NOT_SUPPORTED;
case RTR0MEMOBJTYPE_LOCK:
return VERR_NOT_SUPPORTED; /** @todo implement this... */
break;
case RTR0MEMOBJTYPE_RES_VIRT:
case RTR0MEMOBJTYPE_MAPPING:
default:
return VERR_INTERNAL_ERROR;
}
/*
* Map the ring-0 memory into the current process.
*/
if (!pvR3)
{
if (uAlignment == PAGE_SIZE)
flFlags |= VMDHGP_4MB;
if (fProt & RTMEM_PROT_WRITE)
flFlags |= VMDHGP_WRITE;
if (rc)
return RTErrConvertFromOS2(rc);
}
/*
* Create a mapping object for it.
*/
PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJOS2, Lock), RTR0MEMOBJTYPE_MAPPING, pvR3, pMemToMapOs2->Core.cb);
if (pMemFreeBSD)
{
return VINF_SUCCESS;
}
return VERR_NO_MEMORY;
#endif
return VERR_NOT_IMPLEMENTED;
}
{
{
case RTR0MEMOBJTYPE_PAGE:
case RTR0MEMOBJTYPE_LOW:
case RTR0MEMOBJTYPE_LOCK:
// return pMemFreeBSD->aPages[iPage].Addr;
return NIL_RTHCPHYS;
case RTR0MEMOBJTYPE_CONT:
case RTR0MEMOBJTYPE_PHYS:
case RTR0MEMOBJTYPE_RES_VIRT:
case RTR0MEMOBJTYPE_MAPPING:
default:
return NIL_RTHCPHYS;
}
}