memobj-r0drv-solaris.c revision 257de41d6538efed2a3741b7669531fcf6a50823
/* $Id$ */
/** @file
* IPRT - Ring-0 Memory Objects, Solaris.
*/
/*
* Copyright (C) 2006-2007 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "the-solaris-kernel.h"
#include "memobj-r0drv-solaris.h"
static vnode_t s_PageVnode;
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The Solaris version of the memory object structure.
*/
typedef struct RTR0MEMOBJSOL
{
/** The core structure. */
/** Pointer to kernel memory cookie. */
/** Shadow locked pages. */
void *pvHandle;
/** Access during locking. */
int fAccess;
/** Set if large pages are involved in an RTR0MEMOBJTYPE_PHYS
* allocation. */
bool fLargePage;
/**
* Returns the physical address for a virtual address.
*
* @param pv The virtual address.
*
* @returns The physical address corresponding to @a pv.
*/
{
pfn_t PageFrameNum = 0;
if (SOL_IS_KRNL_ADDR(pv))
else
{
}
}
/**
* Returns the physical address of a page from an array of pages.
*
* @param ppPages The array of pages.
* @param iPage Index of the page in the array to get the physical
* address.
*
* @returns Physical address of specific page within the list of pages specified
* in @a ppPages.
*/
{
AssertReleaseMsg(PageFrameNum != PFN_INVALID, ("rtR0MemObjSolPageToPhys failed. ppPages=%p iPage=%u\n", ppPages, iPage));
}
/**
* Retreives a free page from the kernel freelist.
*
* @param virtAddr The virtual address to which this page maybe mapped in
* the future.
* @param cbPage The size of the page.
*
* @returns Pointer to the allocated page, NULL on failure.
*/
{
if ( !pPage
&& g_frtSolUseKflt)
{
}
return pPage;
}
/**
* Retrieves a free page from the kernel cachelist.
*
* @param virtAddr The virtual address to which this page maybe mapped in
* the future.
* @param cbPage The size of the page.
*
* @return Pointer to the allocated page, NULL on failure.
*/
{
if ( !pPage
&& g_frtSolUseKflt)
{
}
/*
* Remove association with the vnode for pages from the cachelist.
*/
return pPage;
}
/**
* Allocates physical non-contiguous memory.
*
* @param uPhysHi The upper physical address limit (inclusive).
* @param puPhys Where to store the physical address of first page. Optional,
* can be NULL.
* @param cb The size of the allocation.
*
* @return Array of allocated pages, NULL on failure.
*/
{
/** @todo We need to satisfy the upper physical address constraint */
/*
* The page freelist and cachelist both hold pages that are not mapped into any address space.
* The cachelist is not really free pages but when memory is exhausted they'll be moved to the
* free lists, it's the total of the free+cache list that we see on the 'free' column in vmstat.
*
* Reserve available memory for pages and create the pages.
*/
if (rc)
{
if (rc)
{
{
/*
* Get pages from kseg, the 'virtAddr' here is only for colouring but unfortunately
* we don't yet have the 'virtAddr' to which this memory may be mapped.
*/
{
/*
* Get a page from the freelist or cachelist.
*/
if (!pPage)
if (RT_UNLIKELY(!pPage))
{
/*
* No more pages found, release was grabbed so far.
*/
page_create_putback(cPages - i);
while (--i >= 0)
return NULL;
}
}
/*
* We now have the pages locked exclusively, before they are mapped in
* we must downgrade the lock.
*/
if (puPhys)
return ppPages;
}
}
}
return NULL;
}
/**
* Prepares pages allocated by rtR0MemObjSolPagesAlloc for mapping.
*
* @param ppPages Pointer to the page list.
* @param cb Size of the allocation.
* @param auPhys Where to store the physical address of the premapped
* pages.
* @param cPages The number of pages (entries) in @a auPhys.
*
* @returns IPRT status code.
*/
{
{
/*
* Prepare pages for mapping into kernel/user-space. Downgrade the
* exclusive page lock to a shared lock if necessary.
*/
}
return VINF_SUCCESS;
}
/**
* Frees pages allocated by rtR0MemObjSolPagesAlloc.
*
* @param ppPages Pointer to the page list.
* @param cbPages Size of the allocation.
*/
{
{
/*
* We need to exclusive lock the pages before freeing them.
*/
if (!rc)
{
{
/* nothing */;
}
}
}
}
/**
* Allocates a large page to cover the required allocation size.
*
* @param puPhys Where to store the physical address of the allocated
* page. Optional, can be NULL.
* @param cb Size of the allocation.
*
* @returns Pointer to the allocated large page, NULL on failure.
*/
{
/*
* Reserve available memory and create the sub-pages.
*/
if (rc)
{
if (rc)
{
/*
* Get a page off the free list. We set virtAddr to 0 since we don't know where
* the memory is going to be mapped.
*/
if (pRootPage)
{
AssertMsg(!(page_pptonum(pRootPage) & (cPages - 1)), ("%p:%lx cPages=%lx\n", pRootPage, page_pptonum(pRootPage), cPages));
/*
* Mark all the sub-pages as non-free and not-hashed-in.
* It is paramount that we destroy the list (before freeing it).
*/
{
/*
* Ensure page is now be free and the page size-code must match that of the root page.
*/
AssertMsg(pPage->p_szc == pRootPage->p_szc, ("%p - %d expected %d \n", pPage, pPage->p_szc, pRootPage->p_szc));
}
if (puPhys)
return pRootPage;
}
}
}
return NULL;
}
/**
* Prepares the large page allocated by rtR0MemObjSolLargePageAlloc to be mapped.
*
* @param pRootPage Pointer to the root page.
* @param cb Size of the allocation.
*
* @returns IPRT status code.
*/
{
AssertMsg(!(page_pptonum(pRootPage) & (cPages - 1)), ("%p:%lx npages=%lx\n", pRootPage, page_pptonum(pRootPage), cPages));
/*
* We need to downgrade the sub-pages from exclusive to shared locking
* because otherweise we cannot <you go figure>.
*/
{
}
return VINF_SUCCESS;
}
/**
* Frees the page allocated by rtR0MemObjSolLargePageAlloc.
*
* @param pRootPage Pointer to the root page.
* @param cb Allocated size.
*/
{
AssertMsg(!(page_pptonum(pRootPage) & (cPages - 1)), ("%p:%lx cPages=%lx\n", pRootPage, page_pptonum(pRootPage), cPages));
/*
* We need to exclusively lock the sub-pages before freeing the large one.
*/
{
if (!rc)
{
{
/* nothing */;
}
}
}
/*
* Free the large page and unreserve the memory.
*/
}
/**
* Unmaps kernel/user-space mapped memory.
*
* @param pv Pointer to the mapped memory block.
* @param cb Size of the memory block.
*/
{
if (SOL_IS_KRNL_ADDR(pv))
{
}
else
{
}
}
/**
* Lock down memory mappings for a virtual address.
*
* @param pv Pointer to the memory to lock down.
* @param cb Size of the memory block.
* @param fAccess Page access rights (S_READ, S_WRITE, S_EXEC)
*
* @returns IPRT status code.
*/
{
/*
*/
if (!SOL_IS_KRNL_ADDR(pv))
{
faultcode_t rc = as_fault(pProc->p_as->a_hat, pProc->p_as, (caddr_t)pv, cb, F_SOFTLOCK, fPageAccess);
if (rc)
{
LogRel(("rtR0MemObjSolLock failed for pv=%pv cb=%lx fPageAccess=%d rc=%d\n", pv, cb, fPageAccess, rc));
return VERR_LOCK_FAILED;
}
}
return VINF_SUCCESS;
}
/**
* Unlock memory mappings for a virtual address.
*
* @param pv Pointer to the locked memory.
* @param cb Size of the memory block.
* @param fPageAccess Page access rights (S_READ, S_WRITE, S_EXEC).
*/
{
if (!SOL_IS_KRNL_ADDR(pv))
{
}
}
/**
* Maps a list of physical pages into user address space.
*
* @param pVirtAddr Where to store the virtual address of the mapping.
* @param fPageAccess Page access rights (PROT_READ, PROT_WRITE,
* PROT_EXEC)
* @param paPhysAddrs Array of physical addresses to pages.
* @param cb Size of memory being mapped.
*
* @returns IPRT status code.
*/
static int rtR0MemObjSolUserMap(caddr_t *pVirtAddr, unsigned fPageAccess, uint64_t *paPhysAddrs, size_t cb)
{
int rc = VERR_INTERNAL_ERROR;
else
return RTErrConvertFromErrno(rc);
}
{
{
case RTR0MEMOBJTYPE_LOW:
break;
case RTR0MEMOBJTYPE_PHYS:
{
if (pMemSolaris->fLargePage)
else
}
break;
case RTR0MEMOBJTYPE_PHYS_NC:
break;
case RTR0MEMOBJTYPE_PAGE:
break;
case RTR0MEMOBJTYPE_LOCK:
break;
case RTR0MEMOBJTYPE_MAPPING:
break;
case RTR0MEMOBJTYPE_RES_VIRT:
{
else
AssertFailed();
break;
}
case RTR0MEMOBJTYPE_CONT: /* we don't use this type here. */
default:
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
{
/* Create the object. */
PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PAGE, NULL, cb);
if (RT_UNLIKELY(!pMemSolaris))
return VERR_NO_MEMORY;
if (RT_UNLIKELY(!pvMem))
{
return VERR_NO_PAGE_MEMORY;
}
return VINF_SUCCESS;
}
{
/* Create the object */
PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOW, NULL, cb);
if (!pMemSolaris)
return VERR_NO_MEMORY;
/* Allocate physically low page-aligned memory. */
if (RT_UNLIKELY(!pvMem))
{
return VERR_NO_LOW_MEMORY;
}
return VINF_SUCCESS;
}
{
}
DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
{
#if HC_ARCH_BITS == 64
PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS_NC, NULL, cb);
if (RT_UNLIKELY(!pMemSolaris))
return VERR_NO_MEMORY;
if (!pvPages)
{
return VERR_NO_MEMORY;
}
return VINF_SUCCESS;
#else /* 32 bit: */
return VERR_NOT_SUPPORTED; /* see the RTR0MemObjAllocPhysNC specs */
#endif
}
DECLHIDDEN(int) rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
{
PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
if (RT_UNLIKELY(!pMemSolaris))
return VERR_NO_MEMORY;
/*
* Allocating one large page gets special treatment.
*/
if (s_cbLargePage == UINT32_MAX)
{
#if 0 /* currently not entirely stable, so disabled. */
if (page_num_pagesizes() > 1)
else
#endif
}
if ( cb == s_cbLargePage
&& cb == uAlignment
&& PhysHighest == NIL_RTHCPHYS)
{
/*
* Allocate one large page.
*/
{
pMemSolaris->fLargePage = true;
return VINF_SUCCESS;
}
}
else
{
/*
* Allocate physically contiguous memory aligned as specified.
*/
{
pMemSolaris->fLargePage = false;
return VINF_SUCCESS;
}
}
return VERR_NO_CONT_MEMORY;
}
DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
{
/* Create the object. */
PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
if (!pMemSolaris)
return VERR_NO_MEMORY;
/* There is no allocation here, it needs to be mapped somewhere first. */
return VINF_SUCCESS;
}
DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess,
{
/* Create the locking object */
PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
if (!pMemSolaris)
return VERR_NO_MEMORY;
/* Lock down user pages. */
int fPageAccess = S_READ;
if (fAccess & RTMEM_PROT_WRITE)
if (fAccess & RTMEM_PROT_EXEC)
if (RT_FAILURE(rc))
{
return rc;
}
/* Fill in the object attributes and return successfully. */
return VINF_SUCCESS;
}
DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
{
PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, pv, cb);
if (!pMemSolaris)
return VERR_NO_MEMORY;
/* Lock down kernel pages. */
int fPageAccess = S_READ;
if (fAccess & RTMEM_PROT_WRITE)
if (fAccess & RTMEM_PROT_EXEC)
if (RT_FAILURE(rc))
{
return rc;
}
/* Fill in the object attributes and return successfully. */
return VINF_SUCCESS;
}
DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
{
/*
* Use xalloc.
*/
if (RT_UNLIKELY(!pv))
return VERR_NO_MEMORY;
/* Create the object. */
if (!pMemSolaris)
{
LogRel(("rtR0MemObjNativeReserveKernel failed to alloc memory object.\n"));
return VERR_NO_MEMORY;
}
return VINF_SUCCESS;
}
DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
{
return VERR_NOT_SUPPORTED;
}
DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
{
/** @todo rtR0MemObjNativeMapKernel / Solaris - Should be fairly simple alloc kernel memory and memload it. */
return VERR_NOT_SUPPORTED;
}
DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed,
{
/*
* Fend off things we cannot do.
*/
AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
if (uAlignment != PAGE_SIZE)
return VERR_NOT_SUPPORTED;
/*
* Get parameters from the source object.
*/
/*
* Create the mapping object
*/
if (RT_UNLIKELY(!pMemSolaris))
return VERR_NO_MEMORY;
int rc = VINF_SUCCESS;
if (RT_LIKELY(paPhysAddrs))
{
/*
* Prepare the pages according to type.
*/
{
}
else
{
/*
* Have kernel mapping, just translate virtual to physical.
*/
rc = VINF_SUCCESS;
{
{
LogRel(("rtR0MemObjNativeMapUser: no page to map.\n"));
break;
}
}
}
if (RT_SUCCESS(rc))
{
unsigned fPageAccess = PROT_READ;
if (fProt & RTMEM_PROT_WRITE)
if (fProt & RTMEM_PROT_EXEC)
fPageAccess |= PROT_EXEC;
/*
* Perform the actual mapping.
*/
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
{
return VERR_NOT_SUPPORTED;
}
{
{
case RTR0MEMOBJTYPE_PHYS_NC:
{
return rtR0MemObjSolVirtToPhys(pb);
}
case RTR0MEMOBJTYPE_PAGE:
case RTR0MEMOBJTYPE_LOW:
case RTR0MEMOBJTYPE_LOCK:
{
return rtR0MemObjSolVirtToPhys(pb);
}
/*
* Although mapping can be handled by rtR0MemObjSolVirtToPhys(offset) like the above case,
*/
case RTR0MEMOBJTYPE_MAPPING:
case RTR0MEMOBJTYPE_CONT:
case RTR0MEMOBJTYPE_PHYS:
AssertFailed(); /* handled by the caller */
case RTR0MEMOBJTYPE_RES_VIRT:
default:
return NIL_RTHCPHYS;
}
}