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 = 100 * 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 typedef void (*JITWriteFunction)(off_t, const void*, size_t);
96 extern JS_EXPORTDATA JITWriteFunction jitWriteFunction;
98 static inline void* performJITMemcpy(void *dst, const void *src, size_t n)
100 // Use execute-only write thunk for writes inside the JIT region. This is a variant of
101 // memcpy that takes an offset into the JIT region as its destination (first) parameter.
102 if (jitWriteFunction && (uintptr_t)dst >= startOfFixedExecutableMemoryPool && (uintptr_t)dst <= endOfFixedExecutableMemoryPool) {
103 off_t offset = (off_t)((uintptr_t)dst - startOfFixedExecutableMemoryPool);
104 jitWriteFunction(offset, src, n);
108 // Use regular memcpy for writes outside the JIT region.
109 return memcpy(dst, src, n);
112 #else // ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
113 static inline void* performJITMemcpy(void *dst, const void *src, size_t n)
115 return memcpy(dst, src, n);
119 class ExecutableAllocator {
120 enum ProtectionSetting { Writable, Executable };
123 ExecutableAllocator(VM&);
124 ~ExecutableAllocator();
126 static void initializeAllocator();
128 bool isValid() const;
130 static bool underMemoryPressure();
132 static double memoryPressureMultiplier(size_t addedMemoryUsage);
134 #if ENABLE(META_ALLOCATOR_PROFILE)
135 static void dumpProfile();
137 static void dumpProfile() { }
140 RefPtr<ExecutableMemoryHandle> allocate(VM&, size_t sizeInBytes, void* ownerUID, JITCompilationEffort);
142 bool isValidExecutableMemory(const LockHolder&, void* address);
144 static size_t committedByteCount();
146 Lock& getLock() const;
149 #endif // ENABLE(JIT) && ENABLE(ASSEMBLER)
153 #endif // !defined(ExecutableAllocator)