alloc-freebsd.cpp revision 418b9db49fbc652ef9c3f030fdc0f1a322403d95
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/* $Id$ */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/** @file
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * IPRT - Memory Allocation, FreeBSD.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Copyright (C) 2006-2010 Oracle Corporation
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * available from http://www.virtualbox.org. This file is free software;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * you can redistribute it and/or modify it under the terms of the GNU
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * General Public License (GPL) as published by the Free Software
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The contents of this file may alternatively be used under the terms
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * of the Common Development and Distribution License Version 1.0
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * VirtualBox OSE distribution, in which case the provisions of the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * CDDL are applicable instead of those of the GPL.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * You may elect to license modified versions of this file under the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * terms and conditions of either the GPL or the CDDL or both.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*******************************************************************************
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync* Header Files *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync*******************************************************************************/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <iprt/alloc.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <iprt/assert.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <iprt/param.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <iprt/err.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <iprt/string.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <stdlib.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <errno.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <sys/mman.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Allocate first.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
5a3672dca0301427a400aec8734acd70e2cf24a4vboxsync AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cb = RT_ALIGN_Z(cb, PAGE_SIZE);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync void *pv = RTMemPageAlloc(cb);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pv)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Add PROT_EXEC flag to the page(s).
5a3672dca0301427a400aec8734acd70e2cf24a4vboxsync */
5a3672dca0301427a400aec8734acd70e2cf24a4vboxsync memset(pv, 0xcc, cb);
dfd08267d2958ae1cd559dd7dc2f36bf5461648dvboxsync int rc = mprotect(pv, cb, PROT_READ | PROT_WRITE | PROT_EXEC);
5a3672dca0301427a400aec8734acd70e2cf24a4vboxsync if (rc)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync AssertMsgFailed(("mprotect(%p, %#x,,) -> rc=%d, errno=%d\n", pv, cb, rc, errno));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(pv);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync pv = NULL;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return pv;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(void) RTMemExecFree(void *pv, size_t cb) RT_NO_THROW
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pv)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(pv);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cb = RT_ALIGN_Z(cb, PAGE_SIZE);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync void *pv = malloc(cb);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync AssertReleaseMsgReturn(RT_ALIGN_P(pv, PAGE_SIZE) == pv,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ("malloc(%zu) -> %p; expected page aligned!\n", cb, pv),
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync NULL);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return pv;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cb = RT_ALIGN_Z(cb, PAGE_SIZE);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync void *pv = RTMemPageAlloc(cb);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pv)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bzero(pv, RT_ALIGN_Z(cb, PAGE_SIZE));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return pv;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pv)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(pv);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Validate input.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (cb == 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync AssertMsgFailed(("!cb\n"));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return VERR_INVALID_PARAMETER;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync AssertMsgFailed(("fProtect=%#x\n", fProtect));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return VERR_INVALID_PARAMETER;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Convert the flags.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int fProt;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#if RTMEM_PROT_NONE == PROT_NONE \
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync && RTMEM_PROT_READ == PROT_READ \
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync && RTMEM_PROT_WRITE == PROT_WRITE \
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync && RTMEM_PROT_EXEC == PROT_EXEC
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync fProt = fProtect;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#else
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Assert(!RTMEM_PROT_NONE);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (!fProtect)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync fProt = PROT_NONE;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync else
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync fProt = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (fProtect & RTMEM_PROT_READ)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync fProt |= PROT_READ;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (fProtect & RTMEM_PROT_WRITE)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync fProt |= PROT_WRITE;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (fProtect & RTMEM_PROT_EXEC)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync fProt |= PROT_EXEC;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Align the request.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Change the page attributes.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int rc = mprotect(pv, cb, fProt);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (!rc)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return rc;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return RTErrConvertFromErrno(errno);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync