]> git.saurik.com Git - apple/javascriptcore.git/blob - jit/ExecutableAllocator.h
d179f2b4ace365edd05b3e9cfbe0020918f30119
[apple/javascriptcore.git] / jit / ExecutableAllocator.h
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
28 #include "JITCompilationEffort.h"
29 #include <stddef.h> // for ptrdiff_t
30 #include <limits>
31 #include <wtf/Assertions.h>
32 #include <wtf/MetaAllocatorHandle.h>
33 #include <wtf/PageAllocation.h>
34 #include <wtf/PassRefPtr.h>
35 #include <wtf/RefCounted.h>
36 #include <wtf/UnusedParam.h>
37 #include <wtf/Vector.h>
38
39 #if OS(IOS)
40 #include <libkern/OSCacheControl.h>
41 #endif
42
43 #if OS(IOS) || OS(QNX)
44 #include <sys/mman.h>
45 #endif
46
47 #if CPU(MIPS) && OS(LINUX)
48 #include <sys/cachectl.h>
49 #endif
50
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
58 #if OS(WINCE)
59 // From pkfuncs.h (private header file from the Platform Builder)
60 #define CACHE_SYNC_ALL 0x07F
61 extern "C" __declspec(dllimport) void CacheRangeFlush(LPVOID pAddr, DWORD dwLength, DWORD dwFlags);
62 #endif
63
64 #define JIT_ALLOCATOR_LARGE_ALLOC_SIZE (pageSize() * 4)
65
66 #if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
67 #define PROTECTION_FLAGS_RW (PROT_READ | PROT_WRITE)
68 #define PROTECTION_FLAGS_RX (PROT_READ | PROT_EXEC)
69 #define EXECUTABLE_POOL_WRITABLE false
70 #else
71 #define EXECUTABLE_POOL_WRITABLE true
72 #endif
73
74 namespace JSC {
75
76 class JSGlobalData;
77 void releaseExecutableMemory(JSGlobalData&);
78
79 inline size_t roundUpAllocationSize(size_t request, size_t granularity)
80 {
81 if ((std::numeric_limits<size_t>::max() - granularity) <= request)
82 CRASH(); // Allocation is too large
83
84 // Round up to next page boundary
85 size_t size = request + (granularity - 1);
86 size = size & ~(granularity - 1);
87 ASSERT(size >= request);
88 return size;
89 }
90
91 }
92
93 namespace JSC {
94
95 typedef WTF::MetaAllocatorHandle ExecutableMemoryHandle;
96
97 #if ENABLE(ASSEMBLER)
98
99 #if ENABLE(EXECUTABLE_ALLOCATOR_DEMAND)
100 class DemandExecutableAllocator;
101 #endif
102
103 class ExecutableAllocator {
104 enum ProtectionSetting { Writable, Executable };
105
106 public:
107 ExecutableAllocator(JSGlobalData&);
108 ~ExecutableAllocator();
109
110 static void initializeAllocator();
111
112 bool isValid() const;
113
114 static bool underMemoryPressure();
115
116 static double memoryPressureMultiplier(size_t addedMemoryUsage);
117
118 #if ENABLE(META_ALLOCATOR_PROFILE)
119 static void dumpProfile();
120 #else
121 static void dumpProfile() { }
122 #endif
123
124 PassRefPtr<ExecutableMemoryHandle> allocate(JSGlobalData&, size_t sizeInBytes, void* ownerUID, JITCompilationEffort);
125
126 #if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
127 static void makeWritable(void* start, size_t size)
128 {
129 reprotectRegion(start, size, Writable);
130 }
131
132 static void makeExecutable(void* start, size_t size)
133 {
134 reprotectRegion(start, size, Executable);
135 }
136 #else
137 static void makeWritable(void*, size_t) {}
138 static void makeExecutable(void*, size_t) {}
139 #endif
140
141 static size_t committedByteCount();
142
143 private:
144
145 #if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
146 static void reprotectRegion(void*, size_t, ProtectionSetting);
147 #if ENABLE(EXECUTABLE_ALLOCATOR_DEMAND)
148 // We create a MetaAllocator for each JS global object.
149 OwnPtr<DemandExecutableAllocator> m_allocator;
150 DemandExecutableAllocator* allocator() { return m_allocator.get(); }
151 #endif
152 #endif
153
154 };
155
156
157 #else
158
159
160 class ExecutableAllocator {
161 public:
162 static size_t committedByteCount();
163 };
164
165
166 #endif // ENABLE(JIT) && ENABLE(ASSEMBLER)
167
168 } // namespace JSC
169
170 #endif // !defined(ExecutableAllocator)