]> git.saurik.com Git - apple/javascriptcore.git/blame - jit/ExecutableAllocator.h
JavaScriptCore-1218.35.tar.gz
[apple/javascriptcore.git] / jit / ExecutableAllocator.h
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#ifndef ExecutableAllocator_h
27#define ExecutableAllocator_h
6fe7ccc8 28#include "JITCompilationEffort.h"
f9bf01c6 29#include <stddef.h> // for ptrdiff_t
ba379fdc 30#include <limits>
9dae56ea 31#include <wtf/Assertions.h>
6fe7ccc8 32#include <wtf/MetaAllocatorHandle.h>
93a37866 33#include <wtf/MetaAllocator.h>
14957cd0 34#include <wtf/PageAllocation.h>
9dae56ea
A
35#include <wtf/PassRefPtr.h>
36#include <wtf/RefCounted.h>
37#include <wtf/Vector.h>
38
14957cd0 39#if OS(IOS)
ba379fdc 40#include <libkern/OSCacheControl.h>
f9bf01c6
A
41#endif
42
6fe7ccc8
A
43#if OS(IOS) || OS(QNX)
44#include <sys/mman.h>
f9bf01c6
A
45#endif
46
4e4e5a6f
A
47#if CPU(MIPS) && OS(LINUX)
48#include <sys/cachectl.h>
49#endif
50
14957cd0
A
51#if CPU(SH4) && OS(LINUX)
52#include <asm/cachectl.h>
53#include <asm/unistd.h>
54#include <sys/syscall.h>
55#include <unistd.h>
56#endif
57
f9bf01c6
A
58#if OS(WINCE)
59// From pkfuncs.h (private header file from the Platform Builder)
60#define CACHE_SYNC_ALL 0x07F
61extern "C" __declspec(dllimport) void CacheRangeFlush(LPVOID pAddr, DWORD dwLength, DWORD dwFlags);
62#endif
9dae56ea 63
6fe7ccc8 64#define JIT_ALLOCATOR_LARGE_ALLOC_SIZE (pageSize() * 4)
9dae56ea 65
ba379fdc
A
66#if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
67#define PROTECTION_FLAGS_RW (PROT_READ | PROT_WRITE)
68#define PROTECTION_FLAGS_RX (PROT_READ | PROT_EXEC)
14957cd0 69#define EXECUTABLE_POOL_WRITABLE false
ba379fdc 70#else
14957cd0 71#define EXECUTABLE_POOL_WRITABLE true
ba379fdc
A
72#endif
73
74namespace JSC {
75
93a37866
A
76class VM;
77void releaseExecutableMemory(VM&);
78
79static const unsigned jitAllocationGranule = 32;
6fe7ccc8 80
ba379fdc
A
81inline size_t roundUpAllocationSize(size_t request, size_t granularity)
82{
93a37866 83 RELEASE_ASSERT((std::numeric_limits<size_t>::max() - granularity) > request);
ba379fdc
A
84
85 // Round up to next page boundary
86 size_t size = request + (granularity - 1);
87 size = size & ~(granularity - 1);
88 ASSERT(size >= request);
89 return size;
90}
91
92}
93
9dae56ea
A
94namespace JSC {
95
6fe7ccc8
A
96typedef WTF::MetaAllocatorHandle ExecutableMemoryHandle;
97
98#if ENABLE(ASSEMBLER)
14957cd0 99
14957cd0 100#if ENABLE(EXECUTABLE_ALLOCATOR_DEMAND)
6fe7ccc8 101class DemandExecutableAllocator;
14957cd0 102#endif
9dae56ea 103
93a37866
A
104#if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
105#if CPU(ARM) || CPU(ARM64)
106static const size_t fixedExecutableMemoryPoolSize = 16 * 1024 * 1024;
107#elif CPU(X86_64)
108static const size_t fixedExecutableMemoryPoolSize = 1024 * 1024 * 1024;
109#else
110static const size_t fixedExecutableMemoryPoolSize = 32 * 1024 * 1024;
111#endif
112
113extern uintptr_t startOfFixedExecutableMemoryPool;
114#endif
115
9dae56ea 116class ExecutableAllocator {
14957cd0 117 enum ProtectionSetting { Writable, Executable };
ba379fdc 118
9dae56ea 119public:
93a37866 120 ExecutableAllocator(VM&);
6fe7ccc8
A
121 ~ExecutableAllocator();
122
123 static void initializeAllocator();
9dae56ea 124
4e4e5a6f 125 bool isValid() const;
14957cd0
A
126
127 static bool underMemoryPressure();
6fe7ccc8
A
128
129 static double memoryPressureMultiplier(size_t addedMemoryUsage);
130
131#if ENABLE(META_ALLOCATOR_PROFILE)
132 static void dumpProfile();
133#else
134 static void dumpProfile() { }
135#endif
14957cd0 136
93a37866 137 PassRefPtr<ExecutableMemoryHandle> allocate(VM&, size_t sizeInBytes, void* ownerUID, JITCompilationEffort);
9dae56ea 138
ba379fdc
A
139#if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
140 static void makeWritable(void* start, size_t size)
141 {
142 reprotectRegion(start, size, Writable);
143 }
144
145 static void makeExecutable(void* start, size_t size)
146 {
147 reprotectRegion(start, size, Executable);
148 }
149#else
150 static void makeWritable(void*, size_t) {}
151 static void makeExecutable(void*, size_t) {}
152#endif
153
14957cd0 154 static size_t committedByteCount();
ba379fdc 155
9dae56ea 156private:
ba379fdc
A
157
158#if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
14957cd0 159 static void reprotectRegion(void*, size_t, ProtectionSetting);
6fe7ccc8
A
160#if ENABLE(EXECUTABLE_ALLOCATOR_DEMAND)
161 // We create a MetaAllocator for each JS global object.
162 OwnPtr<DemandExecutableAllocator> m_allocator;
163 DemandExecutableAllocator* allocator() { return m_allocator.get(); }
164#endif
ba379fdc
A
165#endif
166
9dae56ea
A
167};
168
f9bf01c6 169
14957cd0
A
170#else
171
93a37866 172#if PLATFORM(IOS)
14957cd0
A
173
174class ExecutableAllocator {
175public:
176 static size_t committedByteCount();
177};
178
93a37866 179#endif // !PLATFORM(IOS)
14957cd0
A
180
181#endif // ENABLE(JIT) && ENABLE(ASSEMBLER)
9dae56ea 182
6fe7ccc8
A
183} // namespace JSC
184
9dae56ea 185#endif // !defined(ExecutableAllocator)