memobj-r0drv-solaris.c revision c309d3381b09f960d633d31171be3fd10fb4e4bb
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * IPRT - Ring-0 Memory Objects, Solaris.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Copyright (C) 2006-2007 Sun Microsystems, Inc.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * This file is part of VirtualBox Open Source Edition (OSE), as
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * available from http://www.virtualbox.org. This file is free software;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * you can redistribute it and/or modify it under the terms of the GNU
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * General Public License (GPL) as published by the Free Software
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Foundation, in version 2 as it comes in the "COPYING" file of the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The contents of this file may alternatively be used under the terms
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * of the Common Development and Distribution License Version 1.0
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * VirtualBox OSE distribution, in which case the provisions of the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * CDDL are applicable instead of those of the GPL.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * You may elect to license modified versions of this file under the
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * terms and conditions of either the GPL or the CDDL or both.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Clara, CA 95054 USA or visit http://www.sun.com if you need
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * additional information or have any questions.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/*******************************************************************************
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico* Header Files *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico*******************************************************************************/
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico/*******************************************************************************
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico* Structures and Typedefs *
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico*******************************************************************************/
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * The Solaris version of the memory object structure.
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /** The core structure. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /** Pointer to kernel memory cookie. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /** Shadow locked pages. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * Used for supplying the solaris kernel info. about memory limits
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * during contiguous allocations (i_ddi_mem_alloc)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico (uint64_t)0xffffffff, /* high limit (32-bit PA, 4G) */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico 1, /* scatter-gather list length (1 for contiguous) */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico 0 /* bus-specific flags */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicostatic uint64_t rtR0MemObjSolarisVirtToPhys(struct hat* hatSpace, caddr_t virtAddr)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* We could use paddr_t (more solaris-like) rather than uint64_t but paddr_t isn't defined for 64-bit */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgFailed(("rtR0MemObjSolarisVirtToPhys: hat_getpfnum for %p failed.\n", virtAddr));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint64_t physAddr = ((uint64_t)pfn << MMU_PAGESHIFT) | ((uintptr_t)virtAddr & MMU_PAGEOFFSET);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pMemSolaris->Core.u.Lock.R0Process == NIL_RTR0PROCESS)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico addrSpace = ((proc_t *)pMemSolaris->Core.u.Lock.R0Process)->p_as;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico as_pageunlock(addrSpace, pMemSolaris->ppShadowPages, pMemSolaris->Core.pv, pMemSolaris->Core.cb, S_WRITE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pMemSolaris->Core.u.Mapping.R0Process == NIL_RTR0PROCESS)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Kernel process*/
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* User process */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico proc_t *userProc = (proc_t *)pMemSolaris->Core.u.Mapping.R0Process;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico hat_unload(hatSpace, pMemSolaris->Core.pv, pMemSolaris->Core.cb, HAT_UNLOAD_UNLOCK);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico as_unmap(addrSpace, pMemSolaris->Core.pv, pMemSolaris->Core.cb);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* unused */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgFailed(("enmType=%d\n", pMemSolaris->Core.enmType));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Create the object */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PAGE, NULL, cb);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico void *virtAddr = ddi_umem_alloc(cb, DDI_UMEM_SLEEP, &pMemSolaris->Cookie);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Try page alloc first */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = rtR0MemObjNativeAllocPage(ppMem, cb, fExecutable);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico while (iPage-- > 0)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (rtR0MemObjNativeGetPagePhysAddr(*ppMem, iPage) > (_4G - PAGE_SIZE))
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Failed! Fall back to physical contiguous alloc */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = rtR0MemObjNativeAllocCont(ppMem, cb, fExecutable);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Create the object */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_CONT, NULL, cb);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Allocate physically contiguous page-aligned memory. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = i_ddi_mem_alloc(NULL, &g_SolarisX86PhysMemLimits, cb, 1, 0, NULL, &virtAddr, NULL, NULL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pMemSolaris->Core.u.Cont.Phys = rtR0MemObjSolarisVirtToPhys(kas.a_hat, virtAddr);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /** @todo rtR0MemObjNativeAllocPhysNC / solaris */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return VERR_NOT_SUPPORTED; /* see the RTR0MemObjAllocPhysNC specs */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgReturn(PhysHighest >= 16 *_1M, ("PhysHigest=%VHp\n", PhysHighest), VERR_NOT_IMPLEMENTED);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rtR0MemObjNativeAllocCont(ppMem, cb, false);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Create the object */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* There is no allocation here, it needs to be mapped somewhere first */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertReturn(R0Process == RTR0ProcHandleSelf(), VERR_INVALID_PARAMETER);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Create the locking object */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Lock down user pages */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = as_pagelock(useras, &ppl, (caddr_t)R3Ptr, cb, S_WRITE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pMemSolaris->Core.u.Lock.R0Process = (RTR0PROCESS)userproc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico as_pageunlock(useras, ppl, (caddr_t)R3Ptr, cb, S_WRITE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico cmn_err(CE_NOTE, "rtR0MemObjNativeLockUser: as_pagelock failed to get shadow pages\n");
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico cmn_err(CE_NOTE,"rtR0MemObjNativeLockUser: as_pagelock failed rc=%d\n", rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Create the locking object */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, pv, cb);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico caddr_t virtAddr = (caddr_t)((uintptr_t)pv & (uintptr_t)PAGEMASK);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Lock down kernel pages */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico int rc = as_pagelock(&kas, &ppl, virtAddr, cb, S_WRITE);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pMemSolaris->Core.u.Lock.R0Process = NIL_RTR0PROCESS; /* means kernel, see rtR0MemObjNativeFree() */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico cmn_err(CE_NOTE, "rtR0MemObjNativeLockKernel: failed to get shadow pages\n");
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico cmn_err(CE_NOTE,"rtR0MemObjNativeLockKernel: as_pagelock failed rc=%d\n", rc);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment, unsigned fProt)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* @todo rtR0MemObjNativeMapKernel / Solaris - Should be fairly simple alloc kernel memory and memload it. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoint rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PRTR0MEMOBJSOLARIS pMemToMapSolaris = (PRTR0MEMOBJSOLARIS)pMemToMap;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Request the system for a mapping address. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico map_addr(&addr, size, 0 /* offset */, 1 /* vac-align */, MAP_SHARED | MAP_ANONYMOUS);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: map_addr failed\n");
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Check address against alignment, fail if it doesn't match */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: map_addr alignment(%ld) failed.\n", uAlignment);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Our protection masks are identical to <sys/mman.h> but we
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico * need to add PROT_USER for the pages to be accessible by user
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico struct segvn_crargs crArgs = SEGVN_ZFOD_ARGS(fProt | PROT_USER, PROT_ALL);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico rc = as_map(useras, addr, size, segvn_create, &crArgs);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: as_map failure.\n");
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Create the mapping object */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_MAPPING, pv, size);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Undo mapping on failure. */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico /* Map each page into user space */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico page_t *pp = page_numtopp_nolock(hat_getpfnum(kas.a_hat, kernAddr));
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico hat_memload(useras->a_hat, pageAddr, pp, (fProt | PROT_USER), HAT_LOAD_LOCK);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico pMemSolaris->Core.u.Mapping.R0Process = (RTR0PROCESS)userproc;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNicoRTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return rtR0MemObjSolarisVirtToPhys(kas.a_hat, pb);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico if (pMemSolaris->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico proc_t *userProc = (proc_t *)pMemSolaris->Core.u.Lock.R0Process;
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico else /* Kernel */
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pMemSolaris->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
784a17b07dd7b7acbc0499b915a969f6937533fdJazzyNico return pMemSolaris->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);