2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef ExecutableAllocator_h
27 #define ExecutableAllocator_h
28 #include "JITCompilationEffort.h"
29 #include <stddef.h> // for ptrdiff_t
31 #include <wtf/Assertions.h>
33 #include <wtf/MetaAllocatorHandle.h>
34 #include <wtf/MetaAllocator.h>
35 #include <wtf/PageAllocation.h>
36 #include <wtf/RefCounted.h>
37 #include <wtf/Vector.h>
40 #include <libkern/OSCacheControl.h>
47 #if CPU(MIPS) && OS(LINUX)
48 #include <sys/cachectl.h>
51 #if CPU(SH4) && OS(LINUX)
52 #include <asm/cachectl.h>
53 #include <asm/unistd.h>
54 #include <sys/syscall.h>
58 #define JIT_ALLOCATOR_LARGE_ALLOC_SIZE (pageSize() * 4)
60 #define EXECUTABLE_POOL_WRITABLE true
66 static const unsigned jitAllocationGranule = 32;
68 typedef WTF::MetaAllocatorHandle ExecutableMemoryHandle;
72 #if ENABLE(EXECUTABLE_ALLOCATOR_DEMAND)
73 class DemandExecutableAllocator;
76 #if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
78 static const size_t fixedExecutableMemoryPoolSize = 16 * 1024 * 1024;
80 static const size_t fixedExecutableMemoryPoolSize = 32 * 1024 * 1024;
82 static const size_t fixedExecutableMemoryPoolSize = 1024 * 1024 * 1024;
84 static const size_t fixedExecutableMemoryPoolSize = 32 * 1024 * 1024;
87 static const double executablePoolReservationFraction = 0.15;
89 static const double executablePoolReservationFraction = 0.25;
92 extern JS_EXPORTDATA uintptr_t startOfFixedExecutableMemoryPool;
93 extern JS_EXPORTDATA uintptr_t endOfFixedExecutableMemoryPool;
95 #if ENABLE(SEPARATED_WX_HEAP)
96 extern JS_EXPORTDATA uintptr_t jitWriteFunctionAddress;
98 #endif // ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
100 static inline void* performJITMemcpy(void *dst, const void *src, size_t n)
102 #if ENABLE(SEPARATED_WX_HEAP)
103 // Use execute-only write thunk for writes inside the JIT region. This is a variant of
104 // memcpy that takes an offset into the JIT region as its destination (first) parameter.
105 if (jitWriteFunctionAddress && (uintptr_t)dst >= startOfFixedExecutableMemoryPool && (uintptr_t)dst <= endOfFixedExecutableMemoryPool) {
106 using JITWriteFunction = void (*)(off_t, const void*, size_t);
107 JITWriteFunction func = (JITWriteFunction)jitWriteFunctionAddress;
108 off_t offset = (off_t)((uintptr_t)dst - startOfFixedExecutableMemoryPool);
109 func(offset, src, n);
114 // Use regular memcpy for writes outside the JIT region.
115 return memcpy(dst, src, n);
118 class ExecutableAllocator {
119 enum ProtectionSetting { Writable, Executable };
122 ExecutableAllocator(VM&);
123 ~ExecutableAllocator();
125 static void initializeAllocator();
127 bool isValid() const;
129 static bool underMemoryPressure();
131 static double memoryPressureMultiplier(size_t addedMemoryUsage);
133 #if ENABLE(META_ALLOCATOR_PROFILE)
134 static void dumpProfile();
136 static void dumpProfile() { }
139 RefPtr<ExecutableMemoryHandle> allocate(VM&, size_t sizeInBytes, void* ownerUID, JITCompilationEffort);
141 bool isValidExecutableMemory(const LockHolder&, void* address);
143 static size_t committedByteCount();
145 Lock& getLock() const;
148 #endif // ENABLE(JIT) && ENABLE(ASSEMBLER)
152 #endif // !defined(ExecutableAllocator)