michael@0: /* michael@0: * Copyright (C) 2008 Apple Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR michael@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY michael@0: * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #include "assembler/jit/ExecutableAllocator.h" michael@0: michael@0: #if ENABLE_ASSEMBLER && WTF_OS_UNIX && !WTF_OS_SYMBIAN michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "assembler/wtf/Assertions.h" michael@0: #include "assembler/wtf/VMTags.h" michael@0: #include "js/Utility.h" michael@0: michael@0: namespace JSC { michael@0: michael@0: size_t ExecutableAllocator::determinePageSize() michael@0: { michael@0: return getpagesize(); michael@0: } michael@0: michael@0: ExecutablePool::Allocation ExecutableAllocator::systemAlloc(size_t n) michael@0: { michael@0: void *allocation = mmap(NULL, n, INITIAL_PROTECTION_FLAGS, MAP_PRIVATE | MAP_ANON, VM_TAG_FOR_EXECUTABLEALLOCATOR_MEMORY, 0); michael@0: if (allocation == MAP_FAILED) michael@0: allocation = NULL; michael@0: ExecutablePool::Allocation alloc = { reinterpret_cast(allocation), n }; michael@0: return alloc; michael@0: } michael@0: michael@0: void ExecutableAllocator::systemRelease(const ExecutablePool::Allocation& alloc) michael@0: { michael@0: int result = munmap(alloc.pages, alloc.size); michael@0: ASSERT_UNUSED(result, !result); michael@0: } michael@0: michael@0: #if WTF_ENABLE_ASSEMBLER_WX_EXCLUSIVE michael@0: void ExecutableAllocator::reprotectRegion(void* start, size_t size, ProtectionSetting setting) michael@0: { michael@0: if (!pageSize) michael@0: intializePageSize(); michael@0: michael@0: // Calculate the start of the page containing this region, michael@0: // and account for this extra memory within size. michael@0: intptr_t startPtr = reinterpret_cast(start); michael@0: intptr_t pageStartPtr = startPtr & ~(pageSize - 1); michael@0: void* pageStart = reinterpret_cast(pageStartPtr); michael@0: size += (startPtr - pageStartPtr); michael@0: michael@0: // Round size up michael@0: size += (pageSize - 1); michael@0: size &= ~(pageSize - 1); michael@0: michael@0: mprotect(pageStart, size, (setting == Writable) ? PROTECTION_FLAGS_RW : PROTECTION_FLAGS_RX); michael@0: } michael@0: #endif michael@0: michael@0: #if WTF_CPU_ARM_TRADITIONAL && WTF_OS_LINUX && WTF_COMPILER_RVCT michael@0: __asm void ExecutableAllocator::cacheFlush(void* code, size_t size) michael@0: { michael@0: ARM michael@0: push {r7} michael@0: add r1, r1, r0 michael@0: mov r7, #0xf0000 michael@0: add r7, r7, #0x2 michael@0: mov r2, #0x0 michael@0: svc #0x0 michael@0: pop {r7} michael@0: bx lr michael@0: } michael@0: #endif michael@0: michael@0: void michael@0: ExecutablePool::toggleAllCodeAsAccessible(bool accessible) michael@0: { michael@0: char* begin = m_allocation.pages; michael@0: size_t size = m_freePtr - begin; michael@0: michael@0: if (size) { michael@0: // N.B. Some systems, like 32bit Mac OS 10.6, implicitly add PROT_EXEC michael@0: // when mprotect'ing memory with any flag other than PROT_NONE. Be michael@0: // sure to use PROT_NONE when making inaccessible. michael@0: int flags = accessible ? PROT_READ | PROT_WRITE | PROT_EXEC : PROT_NONE; michael@0: if (mprotect(begin, size, flags)) michael@0: MOZ_CRASH(); michael@0: } michael@0: } michael@0: michael@0: } michael@0: michael@0: #endif // HAVE(ASSEMBLER)